Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Id Generation possibilities

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic