gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:36:42 25 4
gpt4 key购买 nike

我正在使用 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>

最佳答案

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

关于hibernate - 无法在 postgres 数据库中使用 hibernate 注释创建唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com