简体   繁体   中英

Hibernate OneTOMany Mapping Error

I have 2 classes called Enquiry.java and EnquiryItem.java

When an enuiry is generated i am saving its items in EnquiryItems.

Below is my mapping files in POJO classes.

Enquiry.java

@OneToMany(fetch=FetchType.LAZY,cascade = CascadeType.ALL,mappedBy="enquiryId")
@JoinColumn(name = "enq_id",  referencedColumnName = "id")
private List<EnquiryItem> enquiryItems=new ArrayList<EnquiryItem>();

EnquiryItem.java

@ManyToOne
@JoinColumn(name="enq_id")
private Enquiry enquiryId;

I am unable to locate where I am doing wrong. Please help me.

EDIT: when an enquiry is saved the enquiry_id is saving null in database.

The problem is you need to set the enquiryId before save it in to database. This required as you have used mappedBy attribute. what you need to do is.

When you are adding EnquiryItem object to the list please set the enquiryId by calling it's setter method. EDIT:

like below:

EnquiryItem.setEnquiryId(enquiry )

EDIT 2:

Enquiry enquiry= new Enquiry(bus,prListNo,enquiryNo,user,new java.util.Date(),enquiryItemsList); 
for(EnquiryItem item:enquiryItemsList)
 item.setEnquiryId(enquiry);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM