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
无法在postgres数据库中使用hibernate批注创建唯一约束_Hibernate_Postgresql_Annotations_Unique Constraint - Fatal编程技术网

无法在postgres数据库中使用hibernate批注创建唯一约束

无法在postgres数据库中使用hibernate批注创建唯一约束,hibernate,postgresql,annotations,unique-constraint,Hibernate,Postgresql,Annotations,Unique Constraint,我正在使用hibernate,postgres 9.1,我正在尝试通过hibernate注释添加一个唯一的约束(不在数据库中显式添加) 但是,db忽略唯一注释,不创建约束。我还尝试在@Table注释中添加约束,但也失败了 @Entity public class Person implements Serializable { @Id @Column(insertable=false, updatable=false) @Type(type="java.lang.Long") @

我正在使用hibernate,postgres 9.1,我正在尝试通过hibernate注释添加一个唯一的约束(不在数据库中显式添加)

但是,db忽略唯一注释,不创建约束。我还尝试在@Table注释中添加约束,但也失败了

@Entity
public class Person implements Serializable {   

 @Id
 @Column(insertable=false, updatable=false)
 @Type(type="java.lang.Long")
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PERSON_SEQ")
 @SequenceGenerator(name="PERSON_SEQ", sequenceName="PERSON_SEQ", allocationSize=1)
 private Long id;


 @Column(unique=true)
 private String username;
}
hibernate.cfg.xml

<hibernate-configuration>
<session-factory>

    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Test1</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">postgres</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

    <property name="hbm2ddl.auto">update</property>

</session-factory>

org.postgresql.Driver
jdbc:postgresql://localhost:5432/Test1
博士后
博士后
org.hibernate.dialogue.PostgreSqlDialogue
更新

只需将属性“hbm2ddl.auto”设置为“创建”,hibernate将使用唯一列重新创建表。

只需将属性“hbm2ddl.auto”设置为“创建”,hibernate将使用唯一列重新创建表。

您是如何确定该约束被忽略的?该约束不会传递给db。另外,我对它进行了测试,它允许我创建重复的用户名。您如何确定该约束被忽略?该约束不会传递给数据库。另外,我对它进行了测试,它允许我创建重复的用户名