Search code examples
hibernateuuid

How to let hibernate don't override the specified UUID?


I defined a entity with a uuid primary key:

@Entity
@Table(name = "SITES")
public class Site extends Model {

    @Id
    @Column(length = 32)
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;
}

It works fine, but I found hibernate will always override the specified id:

When I save a Site object with specified uuid 402881e533fe2de70133fe2df6790000, like:

Site site = new Site();
site.setId("402881e533fe2de70133fe2df6790000");
siteDao.save(site);

Print the id:

System.out.println(site.id);

Get:

402881e534110dc30134110dd4e80000

Is there any way to ask hibernate don't override my specified uuid?


Solution

  • Why you want to use setId(). You should let Hibernate handle the ids and everything is right.

    Hibernate sets the id if you persist a new Object.