Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2011-12-06 02:56:46

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?

Questioner
Freewind
Viewed
0
KaiThomasWerther 2011-12-06 17:49:26

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.