我有一个实体需要拥有UUID类型的唯一密钥(非主要密钥)。
@Entity
public class MyEntity {
@Id
@NotNull
@GeneratedValue(strategy = SEQUENCE, generator = "seq_entity")
@SequenceGenerator(name = "seq_entity", sequenceName = "seq_entity", allocationSize = 1)
private Long id;
@NotNull
@Type(type = "pg-uuid")
@Column(name = "uu_id", unique = true)
private UUID uuid;
@NotNull
@Size(max = 30)
private String name;
// gets and sets
}
当我坚持这个实体时,在我的DAO课程中可以看到如下:
@Transactional
public class EntityDAO {
@Inject
private EntityManager em;
public void insert(MyEntity myEntity) { //myEntity comes only with name attribute
myEntity.setUUID(UUID.randomUUID()); //I'd like to generate automatically by the database
em.persist(myEntity);
}
}
是否在数据库中插入,但控制台上出现以下错误:
09:09:43,529 SEVERE [br.gov.frameworkdemoiselle.exception] (http-/127.0.0.1:8080-1) Erro interno do servidor: org.yaml.snakeyaml.error.YAMLException: No JavaBean properties found in java.util.UUID
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperties(PropertyUtils.java:97)
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperties(PropertyUtils.java:87)
at org.yaml.snakeyaml.representer.Representer.getProperties(Representer.java:243)
答案 0 :(得分:1)
您需要将UID映射到String。如果没有从UID到String的标准映射,则需要使用UserType。
或者替代地只使用String而不是UUID作为属性。
的第3.10章中阅读映射UUID的替代方法您可以使用EntityListener而不是在您可以在@PrePersist方法中设置的服务中设置ID。
@Entity
public class MyEntity {
@PrePersist
private assignUIID(){
myEntity.setUUID(UUID.randomUUID());
}
}