-->
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.  [ 1 post ] 
Author Message
 Post subject: ManyToMany with mappedBy does not work ??
PostPosted: Fri Jan 19, 2007 6:28 am 
Newbie

Joined: Fri May 12, 2006 9:48 am
Posts: 14
Location: Toulouse
Hibernate version: 3.2.1

Hello All,

I have 2 objects: DATASTRIP and COUPLINGINFORMATION
Many Datastrips can be coupled by a CouplingInformation object.
One Datastrip can contain many CouplingInformation objects to be coupled to other Datastrips

Mapping documents:
DATASTRIP:
Code:
@Entity
public class DataStrip implements Serializable {

private Integer dataStripId; //PK
private Collection<CouplingInformation> couplingInformationList = new HashSet<CouplingInformation>();


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="datastrip_id")
public Integer getDataStripId() {
        return dataStripId;
}

@ManyToMany(
   cascade=CascadeType.ALL,
   fetch = FetchType.LAZY,
   targetEntity=CouplingInformation.class
)
@JoinTable(
   name="datastrip_coupling",
   joinColumns={@JoinColumn(name="datastrip_id")},
   inverseJoinColumns={@JoinColumn(name="couplinginformation_id")}
)
public Collection<CouplingInformation> getCouplingInformationList() {
     return couplingInformationList;
}
   
public void setCouplingInformationList(
      Collection<CouplingInformation> couplingInformationList) {
      this.couplingInformationList = couplingInformationList;
}


COUPLINGINFORMATION:

Code:
@Entity
public class CouplingInformation implements Serializable {

private Integer couplingInformationId; //PK
private Collection<DataStrip> dataStripList = new HashSet<DataStrip>();

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="couplinginformation_id")
       public Integer getCouplingInformationId() {
return couplingInformationId;
}
[b]
@ManyToMany(
   cascade=CascadeType.ALL,
   fetch = FetchType.LAZY,
   mappedBy="couplingInformationList",
   targetEntity=DataStrip.class
)
[/b]
public Collection<DataStrip> getDataStripList() {
      return dataStripList;
}

public void setDataStripList(Collection<DataStrip> dataStripList) {
      this.dataStripList = dataStripList;
}


If I do the ManyToMany coupling in CouplingInformation with mappedBy, it does NOT work.
I have a test fuction that does follows:
- I create a CouplingInformation.
- I create 2 DataStrips.
- I add the 2 DS into the CI and persist everything.
After that I check everything is well persisted.
There are 2 DS in the database, there is a new CI in the database.
But there are no entries in the intermediate table 'datastrip_couplinginformation'

After hours of trying, fighting, experimenting I did this:
I replaced the 'mappedBy' property by another @JoinTable Annotation.
Code:
[b]
@ManyToMany(
   cascade=CascadeType.ALL,
   fetch = FetchType.LAZY,
   targetEntity=DataStrip.class
)
@JoinTable(
   name="datastrip_coupling",
   joinColumns={@JoinColumn(name="couplinginformation_id")},
   inverseJoinColumns={@JoinColumn(name="datastrip_id")}
[/b]
public Collection<DataStrip> getDataStripList() {
      return dataStripList;
}


And now the intermediate table is filled in as it should be !!!

Is there any reasonable explenation for such a behaviour ?

I have a secondary problem:
Furtheron the testcode, I take one of the two DataStrips that just got persisted and I do a
assertEqual("couplinginf.size=1", 1, ds.getCouplingInformationList.size());

The test always fails when I do that, it shows 0

If I enhance the code with following:
Code:
public void setDataStripList(Collection<DataStrip> dataStripList) {
      this.dataStripList = dataStripList;
      for (DataStrip datastrip : dataStripList) {
           datastrip.getCouplingInformationList.add(this);
      }
}


The insert operation fails because now Hibernate wants to insert the combination in the intermediate table a second time (and encounters a pk key exception of the db)

How can I make it work that a CI is shown for a DS that is just being added by the same CI ?
Jan


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

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.