无法在postgres数据库中使用hibernate批注创建唯一约束
我正在使用hibernate,postgres 9.1,我正在尝试通过hibernate注释添加一个唯一的约束(不在数据库中显式添加) 但是,db忽略唯一注释,不创建约束。我还尝试在@Table注释中添加约束,但也失败了无法在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") @
@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。另外,我对它进行了测试,它允许我创建重复的用户名。您如何确定该约束被忽略?该约束不会传递给数据库。另外,我对它进行了测试,它允许我创建重复的用户名