use*_*943 6 java sql-server hibernate
我正在使用:Spring 4,Hibernate 4,SQL Server 2008
我知道如何使用SQL Server 2008从这个问题响应中如何创建一个也允许空值的唯一约束?
但是由于我在创建表时没有生成任何手动SQL代码,是否可以通过我的Entity类中的Hibernate注释在我的约束中生成"where子句"?
我的DDL是从头创建的,具有java实体定义,如下所示:
@Entity
@Table(name="Neighborhood", 
uniqueConstraints = {@UniqueConstraint(columnNames = {"codecnbv","zipcode"})})
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
public class Neighborhood implements Serializable {
private String id;
private String codecnbv;
private String zipcode;
@Id 
@Column(name="id", nullable=false, unique=true, length=2)
public String getId() {
    return this.id;
}
@Column(name="codecnbv", nullable=true, length=12) //explicitly nullable
public String getCodecnbv() {
    return codecnbv;
}
@Column(name="zipcode", nullable=true, length=5) //explicitly nullable
public String getZipcode() {
    return zipcode;
}
}
但是,只要我添加数据并尝试在列codecnbv和/或zipcode中输入NULL的第二条记录,我就会收到一个异常,说我违反了唯一约束.
我要求我必须允许多个空值,当值不为空时,我应该有唯一的值,即
对于zipcode列
这不是 Hibernate 的问题,而是 SQL Server 的问题,它考虑NULL一个值并且不允许第二个NULL值。邪恶,我知道。
一些链接:
http://sqlmag.com/database-development/multiple-nulls-unique-constraints
| 归档时间: | 
 | 
| 查看次数: | 7303 次 | 
| 最近记录: |