-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: hibernate 1:n relation, cascading
PostPosted: Mon Jan 25, 2010 5:56 am 
Newbie

Joined: Mon Oct 06, 2008 5:20 am
Posts: 3
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".


Top
 Profile  
 
 Post subject: Re: hibernate 1:n relation, cascading
PostPosted: Mon Jan 25, 2010 8:55 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I guess you want to have one single bidirectional association between POI and Address,
and not 2 unidirectional associations with different meaning, so you have to use the mappedBy property in the @OneToMany annotation.

Code:
@OneToMany(mappedBy = "poiId")



This generates you a different more simple schema for the database,
as there's no need for a separate table mapping the OneToMany association.
Most probably it does also resolve your problem.

Guenther


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.