• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Id Generation possibilities

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic