不能创建在Postgres数据库
问题描述:
进出口使用休眠休眠注释唯一约束,9.1 Postgres的,我试图通过休眠注释(不在数据库中明确加入)不能创建在Postgres数据库
然而,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>
答
只需设置属性 “hbm2ddl.auto” 到 “创造” 和hibernate将重新创建具有唯一列的表。
您是如何确定约束被忽略的? – axtavt 2012-01-08 09:25:13
约束不会传递给数据库。 另外,我测试它,它让我创建重复的用户名 – geo 2012-01-08 09:29:34