wood burning stoves*
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

Multiple Id Generation possibilities

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

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

public class User

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()


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


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

Joined: Feb 05, 2001
Posts: 17249

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.

I agree. Here's the link: http://aspose.com/file-tools
subject: Multiple Id Generation possibilities
Similar Threads
oracle sequence id
Trying to save the object value without using Generation Strategy ?
id value should be sequence or asssigned
Insertion of objects with Sequences + Trigger
ejbcreate return type