Hibernate一对多映射完整性约束违规

时间:2014-12-21 20:58:00

标签: java hibernate nhibernate-mapping one-to-many many-to-one

@Entity
@Table(name = "accommodation")
public class Accommodation {

public Accommodation(){

}

public Accommodation(String name, Boolean status, Def country, Def region, Def town, Def star){
    this.name = name;
    this.status = status;
    this.country = country;
    this.region = region;
    this.town = town;
    this.star = star;
}

private Long id;
private List<AccommodationCategory> accommodationCategoryList = new ArrayList<AccommodationCategory>();

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ACC_ID", unique = true, nullable = false)
public Long getId() {
    return id;
}


@OneToMany(fetch = FetchType.LAZY, mappedBy = "accommodation")
public List<AccommodationCategory> getAccommodationCategoryList() {
    return accommodationCategoryList;
}

public void setAccommodationCategoryList(List<AccommodationCategory> accommodationCategoryList) {
    this.accommodationCategoryList = accommodationCategoryList;
}
}


@Entity
@Table(name = "accommodation_category")
public class AccommodationCategory{

public AccommodationCategory(){}
public AccommodationCategory(String name, Boolean valid) {
    this.name = name;
    this.valid = valid;
}

private Long id;
private String name;
private Boolean valid;
private Accommodation accommodation;



@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ACC_CAT_ID", unique = true, nullable = false)
public Long getId() {
    return id;
}



public Boolean getValid() {
    return valid;
}
public void setValid(Boolean valid) {
    this.valid = valid;
}


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ACC_ID", nullable = false)
public Accommodation getAccommodation() {
    return accommodation;
}

public void setAccommodation(Accommodation accommodation) {
    this.accommodation = accommodation;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

住宿有很多AccommodationCategory,但是当我保存到db我得到

Caused by: org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10115 table: ACCOMMODATION_CATEGORY column: ACC_ID
    at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Table.enforceRowConstraints(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Table.insertSingleRow(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementDML.insertRowSet(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementInsert.getResult(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.StatementDMQL.execute(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]
    at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.3.0.jar:2.3.0]

错误。

1 个答案:

答案 0 :(得分:0)

在Accommodation.By默认长变量具有空值

时,错误相同ACC_ID为空
 @Column(name = "ACC_ID", unique = true, nullable = false)
 public Long getId() {
    return id;
 }