How to use UUIDs with Hibernate as a field?

19,467

Solution 1

try this it may help

@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "uuid", unique = true)
private String uuid;

read this link read hibernate documents it is possible

Solution 2

Check the Javadoc of GeneratedValue:

Provides for the specification of generation strategies for the values of primary keys.

With other words - it is not possible with just an annotation to initialize a 'none ID' attribute.

But you can use @PrePersist:

@PrePersist
public void initializeUUID() {
    if (uuid == null) {
        uuid = UUID.randomUUID().toString();
    }
}

Solution 3

It's not because your UUID is a primary key that it's mandatory to have it annoted with @GeneratedValue.

For example, you can do something like this :

public class MyClass
{
   @Id
   private String uuid;

   public MyClass() {}

   public MyClass (String uuid) {
      this.uuid = uuid;
   }
}

And in your app, generate a UUID before saving your entity :

String uuid = UUID.randomUUID().toString();
em.persist(new MyClass(uuid)); // em : entity manager
Share:
19,467

Related videos on Youtube

Florian Mozart
Author by

Florian Mozart

Updated on September 15, 2022

Comments

  • Florian Mozart
    Florian Mozart over 1 year

    I'm trying to use generated UUIDs without @Id annotation, because my primary key is something else. The application does not generate an UUID, do you have an idea?

    This is my declaration:

    @Column(name = "APP_UUID", unique = true)
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    private String uuid;
    

    I'm using Hibernate 4.3.0 with Oracle10g.

    • rand0m86
      rand0m86 almost 10 years
      Have you found the solution yet? I'm having the same problem.
    • Florian Mozart
      Florian Mozart almost 10 years
      I use the last answer as a fix
    • jlb
      jlb about 9 years
      @FlorianMozart you should accept the last answer
  • Florian Mozart
    Florian Mozart about 10 years
    I used your solution without the @Id annotation. No improvement :/
  • Florian Mozart
    Florian Mozart about 10 years
    Eclipse throws an error:"This class has a composite primary key. It must use an ID class."
  • Florian Mozart
    Florian Mozart about 10 years
    I know the chance to have an UUID twice is very unlikely, but it could happen. That's why I wanted hibernate to manage UUIDs. Thank you for your help
  • Olimpiu POP
    Olimpiu POP over 8 years
    @Engineer - you should add more explanations to your answer. Links can become obsolete. It's definitely appropriate to cite sources, but you should provide a proper, full response.
  • Tobias Liefke
    Tobias Liefke over 8 years
    Hibernate does nothing to prevent an ID - conflict (especially it does not generate a new value if the first value exists in the database already)
  • EpicPandaForce
    EpicPandaForce about 8 years
    Unable to build Hibernate SessionFactory: Caused by: org.hibernate.AnnotationException: Unknown Id.generator: hibernate-uuid
  • Obaid Maroof
    Obaid Maroof about 8 years
    isn't it possible to have it generated without @Id annotation. I dont want my column to be a primary key.
  • dryleaf
    dryleaf over 4 years
    This does not answer the question.