父实体ExcpType.java
@Getter
@Setter
@Entity
@Table(name = "mst_excp_type")
public class ExcpType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "mst_excp_type_id",columnDefinition = "bigint", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
protected Integer mstExcpTypeId;
@Column(name = "type_id")
protected String typeId;
@Column(name = "excp_type_desc")
protected String excpTypeDesc;
@Column(name = "status")
protected String status;
@Column(name = "sequence")
protected Integer sequence;
@Column(name = "excp_type_desc_ar", columnDefinition = "nvarchar(100)")
protected String excpTypeDescAr;
// One-to-one relationship with ExcpFor
@OneToOne(mappedBy = "excpType", cascade = CascadeType.ALL)
private ExcpFor excpFor;
@Embedded
protected AuditDetails auditDetails;
子实体 ExcpFor.java
@Getter
@Setter
@Entity
@Table(name = "mst_excp_for")
public class ExcpFor implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "mst_excp_for_id",columnDefinition = "bigint", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
protected Integer mstExcpForId;
@Column(name = "mst_excp_type_id", unique = true, nullable = false, columnDefinition = "bigint")
protected Integer mstExcpTypeId;
@Column(name = "ln_no")
protected Integer lnNo;
@Column(name = "excp_for_code")
protected String excpForCode;
@Column(name = "excp_for_desc")
protected String excpForDesc;
@Column(name = "excp_for_desc_ar", columnDefinition = "nvarchar(100)")
protected String excpForDescAr;
@OneToOne
@JoinColumn(name = "mst_excp_type_id", referencedColumnName = "mst_excp_type_id", insertable=false, updatable=false)
private ExcpType excpType;
@Embedded
protected AuditDetails auditDetails;
我尝试了 @MapsId 注释,但效果不佳
下面是我在父对象中设置子对象,然后保存父对象的部分。
ExcpType excpType = new ExcpType();
ExcpFor excpFor = new ExcpFor();
excpFor.setExcpForCode(form.getExcpTypeVo().getExcpFor());
excpType.setExcpFor(excpFor);
excpType.setTypeId(form.getExcpTypeVo().getTypeId());
excpType.setExcpTypeDesc(form.getExcpTypeVo().getExcpTypeDesc());
excpType.setExcpTypeDescAr(form.getExcpTypeVo().getExcpTypeDescAr());
excpType.setStatus(form.getExcpTypeVo().getStatus());
excpType.setAuditDetails(audit);
excpTypeDao.saveOrUpdateExcpType(excpType);
下面是我的 Dao 中负责将实体持久保存到数据库的片段。
getHibernateTemplate().saveOrUpdate(excpType);
最后这是我得到的错误
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.eanda.smarttime.model.ExcpFor.mstExcpTypeId
发生错误是因为您在 mst_excp_type_id
表中的
mst_excp_for
列上声明了 NOT NULL约束。但是,当您保存
ExcpFor
实体时,您会将 mstExcpTypeId
字段留空。
要解决问题,请删除代码
@Id
@Column(name = "mst_excp_for_id",columnDefinition = "bigint", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
protected Integer mstExcpForId;
@Column(name = "mst_excp_type_id", unique = true, nullable = false, columnDefinition = "bigint")
protected Integer mstExcpTypeId;
并将
excpType
类中 ExcpFor
字段的描述更改为以下内容:
@Id
@OneToOne
@JoinColumn(name = "mst_excp_type_id", insertable=false, updatable=false)
private ExcpType excpType;