I am new to Spring JPA and struggling to save the object
'Tables'
- Parent table
bookheader
uid int (PK), booknum varchar
- Child table
bookinfo
bookid(FK)(PK), remark varchar
BookHeader.java ...
@Entity
@Table(name="tbookheader", schema="v2")
public class BookHeader implements Serializable{
@Id
@SequenceGenerator(name="seq-gen",sequenceName="v2.tbookheader_uid_seq", allocationSize = 1)
@GeneratedValue(strategy= GenerationType.IDENTITY, generator="seq-gen")
@Column(name = "uid")
private int uid;
@Column(name = "booknum")
private String bookNum;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "bookHeader")
private BookInfo bookInfo;
}
BookInfo.java
@Entity
@Table(name="tbookinfo", schema="v2")
public class BookInfo implements Serializable{
@Id
@Column(name = "bookid")
private int uid;
@OneToOne
@JoinColumn(name="bookid",referencedColumnName="uid")
private BookHeader bookHeader;
private String remark;
}
I am getting object from json and i converted josn to object using GSON.
now i am trying to save it through SPRING JPA
{
BookHeader.setBookNum("tesetbooking3");
BookHeader.setUserDate(new Date());
BookHeader.setUserid(1111);
BookHeader.setCrtDate(new Date());
BookHeader.setCrtUserId(1111);
return bookHeaderRepository.save(BookHeader);
}
But i am getting exception:
ERROR: insert or update on table "tbookinfo" violates foreign key constraint
"fk_tbookinfo_bookid"
Detail: Key (bookid)=(0) is not present in table "tbookheader".
Seems generated primary key not passed from the parent table.
Could somebody help me.
TIA.
Change the
idproperties frominttoInteger.The default value for an
intproperty is0, which is a valid foreign key, but there is no object in the DB with that key.