Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用hibernate注释设置多个字段自动增量_Hibernate_Hibernate Annotations - Fatal编程技术网

如何使用hibernate注释设置多个字段自动增量

如何使用hibernate注释设置多个字段自动增量,hibernate,hibernate-annotations,Hibernate,Hibernate Annotations,我有一个表,其中有UUID(唯一UUID字符串)和srNO(序列号)等字段。我希望自动生成这两个字段。如何使用hibernate注释实现相同的效果?据我所知,在@GeneratedValue或@GenericGenerator字段上可以找到@Id注释。(将它们放在非id字段上不会有任何效果-和/或hibernate会抱怨映射无效) 另一种解决方案是在带有注释的方法中设置这些值,这样您就不必调用assignUuid():hibernate将在持久化实体之前为您执行此操作 @Entity publi

我有一个表,其中有UUID(唯一UUID字符串)和srNO(序列号)等字段。我希望自动生成这两个字段。如何使用hibernate注释实现相同的效果?

据我所知,在
@GeneratedValue
@GenericGenerator
字段上可以找到
@Id
注释。(将它们放在非id字段上不会有任何效果-和/或hibernate会抱怨映射无效)

另一种解决方案是在带有注释的方法中设置这些值,这样您就不必调用
assignUuid()
:hibernate将在持久化实体之前为您执行此操作

@Entity
public class MyEntityWithUUID {

    @Id @GeneratedValue
    private long id;

    @NaturalId
    private String uuid;

    @PrePersist
    public void assignUuid(){
        uuid = generateUuid();
    } 

    ...
}