OiO.lk Blog java Foreign Key not Getting automatically generated in child table
java

Foreign Key not Getting automatically generated in child table


  • I have two entities where the primary key of first entity is the foreign key of the second.
  • When I save the parent, my foreign key is not set, so I want to know why it is not being set automatically.

Parent Entity 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;

Child Entity 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;

I tried the @MapsId annotation but that did not work as well

Below is the part where I am setting the child object in my parent object and then saving the parent object.

        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);

Below is the snippet from my Dao responsible for persisting entity to my DB.

getHibernateTemplate().saveOrUpdate(excpType);

Finally this is the error that I get
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.eanda.smarttime.model.ExcpFor.mstExcpTypeId



You need to sign in to view this answers

Exit mobile version