- У меня есть два объекта, где первичный ключ первого объекта является внешним ключом второго.
- Когда я сохраняю родительский объект, мой внешний ключ не установлен, поэтому я хочу знать, почему он не устанавливается автоматически.
Код: Выделить всё
@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;
Код: Выделить всё
@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;
Ниже приведена часть, в которой я устанавливаю дочерний объект в родительском объекте. а затем сохраните родительский объект.
Код: Выделить всё
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);
Код: Выделить всё
getHibernateTemplate().saveOrUpdate(excpType);Код: Выделить всё
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.eanda.smarttime.model.ExcpFor.mstExcpTypeId Подробнее здесь: https://stackoverflow.com/questions/791 ... hild-table
Мобильная версия