aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate: given an object, how do I find its id? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: given an object, how do I find its id?" Watch "Hibernate: given an object, how do I find its id?" New topic
Author

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

Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145

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

Joined: Jan 10, 2002
Posts: 61764
    
  67

How can you not know which field is the id? You're not using a business field as the primary key, are you?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
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

Joined: Jan 10, 2002
Posts: 61764
    
  67

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

Joined: Mar 01, 2008
Posts: 22
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()?


I gotta keep learning...
Jane Jukowsky
Ranch Hand

Joined: Mar 28, 2009
Posts: 145
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

Joined: Jan 10, 2002
Posts: 61764
    
  67

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

Joined: Mar 28, 2009
Posts: 145
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

Joined: May 18, 2004
Posts: 434
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: given an object, how do I find its id?