Hi, I have some problems with a 1:n relation and cascading:
a point of interest (POI) can have n addresses, the owner should be the POI (ONE side). If I persist a POI, all attached addresses should be persisted as well.
POI File:
Code:
private Collection<LpAddress> lpAddress;
@OneToMany()
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Collection<LpAddress> getLpAddress() {
return lpAddress;
}
public void setLpAddress(Collection<LpAddress> lpAddress) {
this.lpAddress = lpAddress;
}
Address File:
Code:
private Long poiId;
@javax.persistence.Column(name = "poi_id")
@Basic
public Long getPoiId() {
return poiId;
}
public void setPoiId(Long poiId) {
this.poiId = poiId;
}
private LpPoi lpPoi;
@ManyToOne()
@JoinColumn(name = "poi_id", updatable = false, insertable = false)
public LpPoi getLpPoi() {
return lpPoi;
}
public void setLpPoi(LpPoi lpPoi) {
this.lpPoi = lpPoi;
}
If I create a new POI, add a list of addresses, whereby each one has a reference to the POI object and save it,
sess.saveOrUpdate(poi);
I get the error message:
Hibernate: select this_.id as id1_0_, this_.digitallatitude as digitall2_1_0_, this_.digitallongitude as digitall3_1_0_, this_.emails as emails1_0_, this_.hours as hours1_0_, this_.latitude as latitude1_0_, this_.longitude as longitude1_0_, this_.name as name1_0_, this_.poitype as poitype1_0_, this_.pracstring as pracstring1_0_, this_.pricerange as pricerange1_0_, this_.pricestring as pricest12_1_0_, this_.review as review1_0_, this_.url as url1_0_ from public.lp_poi this_ where (this_.name=?)
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into public.lp_poi (digitallatitude, digitallongitude, emails, hours, latitude, longitude, name, poitype, pracstring, pricerange, pricestring, review, url, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into public.lp_address (extras, locality, poi_id, postcode, street, id) values (?, ?, ?, ?, ?, ?)
Hibernate: update public.lp_address set poi_id=? where id=?
WARN - JDBCExceptionReporter - SQL Error: 0, SQLState: 23503
ERROR - JDBCExceptionReporter - Batch-Eintrag 0 update public.lp_address set poi_id='7250' where id='7260' wurde abgebrochen. Rufen Sie 'getNextException' auf, um die Ursache zu erfahren.
WARN - JDBCExceptionReporter - SQL Error: 0, SQLState: 23503
ERROR - JDBCExceptionReporter - ERROR: insert or update on table "lp_address" violates foreign key constraint "foreign_key01"
Detail: Key (poi_id)=(7250) is not present in table "poi".