我有课程:
@Entity
@Table(schema = "master", name = "master_mapping")
public class MasterMapping extends AuditedEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "master_field_id")
private MasterFields masterField;
@JsonIgnore
public MasterFields getMasterField() {
return masterField;
}
public void setMasterField(MasterFields masterField) {
this.masterField = masterField;
}
}
和
@Entity
@Table(schema = "master", name = "master_fields")
public class MasterFields extends AuditedEntity {
private String label;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "masterField")
private List<MasterMapping> mappedFields;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<MasterMapping> getMappedFields() {
return mappedFields;
}
public void setMappedFields(List<MasterMapping> mappedFields) {
this.mappedFields = mappedFields;
}
}
和id
的公共类@MappedSuperclass
public class AuditedEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
我正在尝试使用MasterMapping保存MasterFields对象,但我收到错误:
Hibernate:插入master.master_fields(标签)值(?)
休眠:选择currval('master.master_fields_id_seq')
Hibernate:插入master.master_mapping(master_field_id)值(?)
SQL错误:0,SQLState:23502
错误:“master_field_id”列中的空值违反了非空约束 详细信息:失败的行包含(null)。
javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法执行语句
我正在尝试使用EntityManager.merge()将其保存到db。我看到hibernate无法从MasterFields对象获取生成的id。怎么解决它?