This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to manage multiple instances of the same kind of fields on primefaces form  RSS feed

 
Randy Maddocks
Ranch Hand
Posts: 148
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am struggling with an issue and am hoping someone here might be able to offer some ideas on how I can resolve it. In a nutshell, using NetBeans 8, Primefaces 3.5, JPA 2.1 I have a form organized into specific sections. In 4 sections of this form I am looking to collect information about a person (e.g. name, address, etc. . .). Each of those 4 sections contains the same First Name, Last Name type fields, but each section applies to a different person. My struggle is finding a way to "differentiate" each of those sections in the backing bean, to manage the data (e.g. for persisting and retrieving data to and from the database). I thought I was being smart by instantiating multiple instances of a particular type, each instance applying to each of the 4 sections, and then using the getters and setters to manage those fields, but I ran into trouble when I tried to persist the entity to the database and realized I had created multiple instances of the same Entity "type" which caused a problem with EntityManager. My current focus is now to build a List for each section, but in my ignorance of how the View works with the backing bean I am really struggling trying to put this together. I have not included code as I am hoping this explanation might be a start, but I'd be more than happy to provide code if it helps. Thank you!

Regards,

Randy
 
Randy Maddocks
Ranch Hand
Posts: 148
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I neglected to include a key part of this...Each of those aforementioned 4 sections will need to be a separate record in an Oracle database (so using a "merge" operation would not work). The rest of the form will be a separate record from those 4. Again, I really hope I am making sense!!
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little confused about what you want, but I have the general idea, so here's some information that should help.

First, learn EL. It's something essential not only to JSF but to many other aspects of JEE. EL defines the syntax and behavior that allows access to bean properties even if they are located in arcane places, such as arrrays or Maps or as embedded beans within other embedded beans, and so forth.

If you know EL, you have a formidable tool at your command. But use it wisely. EL is a :censored: to debug, since unlike regular Java code, it's not something you can easily examine or breakpoint. And excessively complex EL risks violating the contract that underpins the MVC paradigm that logic should be located in the backing bean, not in the View template.

Let's say that you have 4 Person Entity instances and you want to display/edit them in a single FORM (JSF, like all HTML-based frameworks can only submit one form per Request).

You can have a master bean and make each Person be an element in a 4-element list like so:



Obviously this is very minimalist, but you get the idea, I hope.

Another way is to make each Person instance be a separately-named property:



And there are still more things you can do, but they get more complex.

Note that JSF works with UI Model objects. JPA works with Persistence Model objects. Since both types of objects are POJOs, you can make one object serve in both capacities, but if that's not convenient, don't hesitate to create new UI objects that are more appropriate and let the backing bean's support/business logic handle the task of presenting the UI model in the desired format.
 
Randy Maddocks
Ranch Hand
Posts: 148
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim, many thanks for your reply, very much appreciated! If I may ask, can you show how your JSF examples would look in the bean itself? Just something to start, so I have an idea. In the meantime I will definitely focus on learning EL - I recognize that it is a weakness of mine and I need to be clear on how it works.

Again, thank you very much sir!
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trvial. The first example, the bean's property would be:


Assuming that I didn't forget the proper syntax for declaring arrays!

I think that the same array-oriented EL also works if you have a List or other ordered collection:




The second example would be like:


Intialize before using. And, of course, since these are (private) JavaBean properties, you do have to supply their get/set methods.
 
Randy Maddocks
Ranch Hand
Posts: 148
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect! I had an idea, but wasn't 100% sure! Thanks once again! :thumbup:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!