aspose file tools*
The moose likes Object Relational Mapping and the fly likes Multiple Id Generation possibilities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Multiple Id Generation possibilities" Watch "Multiple Id Generation possibilities" New topic
Author

Multiple Id Generation possibilities

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Anyone come across this issue. We have a PK field that is either application generated or Trigger generated. Depending on the type, the generation of the ID is different. For instance, if it is type A then the application will assign the value. But if it is type B, then a Before Insert trigger will assign the value.

Currently we just have

@Entity
public class User

@Id
public String userId;


That works for the Type A situations, but for inserting Type B objects you get

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()

thrown

Now, I really don't want to have to implement our own IdentifierGenerator class to make this work, I would assume there must be a way. I was going to assign the userId with a fake "0" but then that would make the trigger not operate since it has an

if .userId ISNULL

and if it is null, then it runs the assignment.

This is a legacy database that is not going to change, and there are no surrogate keys.

Thanks guys

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Looks like the right solution is to implement a IdentifierGenerator class.

However, I went with replicating the Trigger code in my Dao class. It was just two lines of code.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Id Generation possibilities