• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTML form returns entity key -- what's best practice?

 
Jarrod Mennuobe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to Hibernate. Sorry if this is basic. I'm looking for what is considered the best or standard practice for dealing with the following:

I have an HTML form containing dropdown menus. The values these menues return are PK column IDs. To make it more specific, I have a Committee object and Person object. Users can edit a Committee, setting the "Chairperson", "Vicechair", and "Members" (using a multi-select HTML form element).

What's shown in the Web page UI is the "fullname" of the Person object. But what's returned is the Person object's PK ID value.

When the Request gets back to my Action class what do I do then?

Should I do a series of Hibernate loads and queries in order to get the objects identified by the various keys selected in the HTML form? And then call Committee.setChair(), Committee.addMember(), etc? And then call session.update(committee)? It seems like a lot of DB I/O, but maybe that's the way it's done.

Or can I go directly to the COMMITTEE table and update CHAIR_ID, VICECHAIR_ID, etc. I'd also have to update the COMMITTEE_MEMBER many-to-many table. It seems like doing that would defeat the whole pupose of Hibernate.

Any help would be appreciated.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on your mapping. If you have the relationships mapped correctly, you should only have to update the People objects, and all the rest is taken care of for you.

Definitely going back to the database to do lookups just to set instance variables is an extra overhead that you should avoid doing.

Hope that helps in some way.

Mark
 
Jarrod Mennuobe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I'm not sure that you understood my question. Let me rephrase. When we enter the Action class, the only object available is an instance of "Lineup" (in the guise of a Struts ActionForm). Its fields include chair, viceChair, etc., but these fields have not been set yet. There are no Person objects in the picture.

At this stage, the website visitor interacting with the HTML form has provided us with the primary keys of the rows in the PERSON table representing the chair, viceChair, members, and so on. In a non-Hibernate application, we would INSERT or UPDATE a row in the LINEUP table, and we would use the previously mentioned keys to set that row's CHAIR_ID, VICECHAIR_ID, etc.

However, in a Hibernate application, you manipulate the object model in Java, and let Hibernate set CHAIR_ID, VICECHAIR_ID, etc. The hitch is that in a Web application, you don't always have the entire object model available. In the situation that I am describing here, all I have to work with is one piece: the Lineup. So I can't hand that to Hibernate with no Person objects attached to it, and expect Hibernate to intuit how to set CHAIR_ID, VICECHAIR_ID, etc.

If anyone can shed some light on this issue I would be grateful.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, the way it was worded sounded like all that you had where the IDs and no objects. or just one object.

I wonder if you can put the Person Object or such in the Session before showing the user the screen, then when you get back what you get back, you just look it up in the Session and use those Objects, so that you can work with Hibernate and not have to go back to the database.

Mark
 
Jarrod Mennuobe
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That sounds like a good direction. I'll have to look into doing something like that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic