-->
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.  [ 7 posts ] 
Author Message
 Post subject: Inverse=true with Annotations ?
PostPosted: Tue Feb 06, 2007 6:27 am 
Newbie

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

Hi all,

I have a ManyToMany relation between DataStrip and CouplingInformation.

I am testing next scenario:
1. new DS1
2. add new CI to DS1
3. persist DS1
4. get CI-id of CI in DS1

5. get new CI with CI-id of point 4
6 count DS in CI
---- > this should return 1, but it returns 0

I think the problem to resolve this is by adding inverse=true in the classing many-to-many mapping of DataStrip

How do I invoke a similar behaviour with Annotations ?
I tried it by adding the DS in the collections of DS of each CI in DS
But that screws it up totally.
(For one-to-many, it works perfectly when the mother is added at each child of the collection in the function setChildCollection)

Mapping documents:
I add them when necessary for the solution

Many thanks in advance

(ps: I DO rate when someone provides a right answer)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 7:05 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I think the "inverse=true" in annotation is to annotate the inverse side with

Code:
@ManyToMany(mappedBy="<?>")


Where in your example mappedBy="<?>" will either by

1) mappedBy="couplingInfromation" if defined in DataStrip
2) mappedBy="dataStrip" if defined in CouplingInformation

i have assumed you have called the getters getDataStrip and getCouplingInformation


Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 10:47 am 
Newbie

Joined: Fri May 12, 2006 9:48 am
Posts: 14
Location: Toulouse
Hi Andi (and others),

I tried that one before.
First I had this:

DataStrip:
Code:
@ManyToMany(
   fetch = FetchType.LAZY,
   targetEntity=DataStrip.class,
   cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH}
)
@JoinTable(
   name="datastrip_coupling",
   joinColumns={@JoinColumn(name="couplinginformation_id")},
   inverseJoinColumns={@JoinColumn(name="datastrip_id")}
)
public Collection<DataStrip> getDataStripList() {
   return dataStripList;
}

CouplingInformation:
Code:
@ManyToMany(
   fetch = FetchType.LAZY,
   targetEntity=CouplingInformation.class,
   cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH},
   mappedBy="couplingInformationList"
)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Collection<CouplingInformation> getCouplingInformationList() {
   return couplingInformationList;
}


When I do next scenario:
1. 1. new DS1
2. add new CI to DS1
3. persist DS1
4. get CI-id of CI in DS1
5. get new CI with CI-id of point 4 (fresh object)
--> Everything works fine

When I do the reverse:
1. new CI
2. add new DS to CI
3. persist CI
4. get DS-id of DS in CI
5. get new DS with DS-id
--> NULL: the intermediate table was not filled in !!

So the solution I got to resolve this was:
CouplingInformation:
Code:
@ManyToMany(
   fetch = FetchType.LAZY,
   targetEntity=CouplingInformation.class,
   cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH}
)
@JoinTable(
   name="datastrip_coupling",
   joinColumns={@JoinColumn(name="datastrip_id")},
   inverseJoinColumns={@JoinColumn(name="couplinginformation_id")}
)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public Collection<CouplingInformation> getCouplingInformationList() {
   return couplingInformationList;
}


Note that in both versions of CouplingInformation I did not succeed in
having size=1 of DS in a CI when a CI was persisted in a DS (scenario in first posting)

So, unless I am missing something fundamentally, this does not do the trick !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 11:16 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

Have you set you entities up like so:

Datastrip entity
Code:
@Entity
public class DataStrip {
......
   // NOTE this is usually a set !!!!
   private Collection<CouplingInformation> mCoupInfo = new ArrayList<CouplingInformation>();

....
   @ManyToMany(
      fetch = FetchType.LAZY,
      targetEntity=DataStrip.class,
      cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REFRESH}
   )
   @JoinTable(
      name="datastrip_coupling",
      joinColumns={@JoinColumn(name="couplinginformation_id")},
      inverseJoinColumns={@JoinColumn(name="datastrip_id")}
   ) 
   public Collection<CouplingInformation> getCouplingInformation() {
      return mCoupInfo;
   }

   // similar setter
}


CouplingInfomation entity
Code:
@Entity
public class CouplingInformation {
......

   private Collection<DataStrip> mDataStrips = new ArrayList<DataStrip>();

....
   @ManyToMany(mappedBy="couplingInformation")
   public Collection<DataStrip> getDataStrips() {
      return mDataStrips;
   }

   // similar setter
}


From what i can gather you are setting the mappedBy to a getter in the same entity, is that correct ??

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:22 pm 
Newbie

Joined: Fri May 12, 2006 9:48 am
Posts: 14
Location: Toulouse
Hi Andi,

Your right, My POJOs are exactly like this in my code (with ArrayList) !
I made the stupid mistake by reversing the content of each POJO: content of DataStrip is the one that is put under CouplingInformation in the posting.
How silly of me.

Any idea of the inverse problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 10:25 pm 
Newbie

Joined: Fri Feb 02, 2007 6:55 am
Posts: 8
With my understanding, if you want to persist CI and get DS1 also persisted in the collection, mappedBy should defined/annotated in DS1 end.

Bidi ManyToMany collection, mappedBy defined the "NON-owning" side.
Only the owning side can persist the relation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 5:30 am 
Newbie

Joined: Fri May 12, 2006 9:48 am
Posts: 14
Location: Toulouse
Hi zarick,

That explains why the CI - DS persistance does not work with "mappedBy" property.
And that's why I had to do it both ways with @JoinTable.

Anyway, the question remains:
In next scenario:
1. new DS
2. add new CI to DS
3. persist DS
4. get CI-id of CI in DS

5. get new CI with CI-id of point 4
6 count DS in CI
---- > this should return 1, but it returns 0

How do I get the DS out of CI ?

Even if I retrieve a fresh CI from the DB after persisting the DS-CI
combination, the DS is not loaded in the CI (I think Hibernate is taking
the CI object from caching instead of doing a real load from DB)

[/url]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.