This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: given an object, how do I find its id?

 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alternatively: given an object, how do I know whether another object with the same id exists in the database? (but without knowing which fields constitute the object's id?

The only solution I can think of is parsing annotations and using reflection - yikes! There has to be an easier way.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64607
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you not know which field is the id? You're not using a business field as the primary key, are you?
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a framework, and yes, any field or a combination of fields may be an id.

Why, is it bad style to use business fields as primary key? Say, a varchar employee id? Are you making the point that every table must have a numeric key?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64607
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm hardly the local database pundit, but I've always adhered to the principal that a business field should never be used as a primary key. You'd think that something like an employee ID would be safe to use, but what if (just as with social security numbers) they decide to start re-using them?

For primary keys I always use a Long value that has no meaning other than to serve as a primary key.
 
Steven Jiang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane Dodo wrote:
Alternatively: given an object, how do I know whether another object with the same id exists in the database? (but without knowing which fields constitute the object's id?

The only solution I can think of is parsing annotations and using reflection - yikes! There has to be an easier way.


Not sure can answer your question.

can you merge it first, then use session.getIdentifier()?
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, maybe I should make it clear what exactly I am trying to do.

I am writing a Swing framework that edits an entity object. It works in two modes: creating new object, and editing an existing one. If it's creating a new object, and user enters an id that already exists in the database, the framework is to fail gracefully (i.e. tell the user that an object with this id already exists, please choose another id).

Unfortunately, it fails with a ConstraintViolationException, and, short of parsing the exception, it's impossible (as far as I can tell) to know whether a primary key or somethiing else has been violated.

If I do a Session.merge(), it will save to the database. I can always rollback that save (although undesirable); however, once it saved, how do I know whether a record already existed before merge?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64607
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the advantages of a surrogate key, is that it doesn't exist until after an object has been persisted. So it's easy to know if an object is in "edit" or "create" mode. No id, means "create".
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but it also complicates debugging. There is something to be said for human-readable keys. They spare you a few joins.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked into using Natural-Id's? I haven't used them much myself but they might help in this scenario

Natural Id's in Hibernate Reference
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic