-->
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.  [ 5 posts ] 
Author Message
 Post subject: HOW TO USE inverse=false with ANNOTATIONS??????
PostPosted: Tue Aug 23, 2005 12:59 pm 
Newbie

Joined: Thu Aug 18, 2005 12:24 pm
Posts: 11
Hibernate version:3.1


Hi everyone..

I want to use the inverse property on a collection in a OneToMany relation.. how can I do this with annotations?

Can anyone give me some complete tutorial of hibernate annotations ?

thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 3:46 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
You can see Hibernate Annotations docs and tests to understand how it works. Not sure right now, but, the side that describes mapping is inverse false. The docs explain it.

valeuz...

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 5:35 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
"??????" is not mandatory when to post a question.
the inverse side in the one where mappedBy is, so not having mappedBy on a collection make in inverse=false.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 10:18 am 
Newbie

Joined: Thu Jan 05, 2006 9:36 am
Posts: 2
I have a problem similar to that described here:

I have entity A with primary key A_ID and
B with primary key B_ID and foreign key PK_A_ID to A.
I want to map those two entities half-bidirectionaly(explained later why so):

Code:
@Entity
public class A {
  Long id;
  Set bs; // 'bs' as plural of 'b'

  @Id
  @Column(name="A_ID")
  public Long getId() { return id; }

  @OneToMany
  @JoinColumn(name = "PK_A_ID")
  public Set getBs() { return bs; }
}

@Entity
public class B {
  Long id;
  Long aId; // a not mapped as whole entity, only a's id is mapped

  @Id
  @Column(name="B_ID")
  public Long getId() { return id; }

  @Column(name="A_ID")
  public Long getAId() { return aId; }
}


The problem is: if I create instance of B and add it to 'bs' set of a (that is some instance of A) and then save it ...
Code:
A a = new A(); B b = new B();
a.getBs().add(b);
b.setAId(a.getId());

... I get additional update of B.PK_A_ID. Setting inverse="true", that was possible in non-annotation mapping, would prevent this aditional update.

Why is this type of half-bidirectional mapping good for me: I have one use case where I create Bs and save them
and n use cases where I only need B.aId, not the whole B.a.

So the real question here is: does the ...
emmanuel wrote:
"??????" is not mandatory when to post a question.
the inverse side in the one where mappedBy is, so not having mappedBy on a collection make in inverse=false.

... mean there is no option to specify inverse="true"?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 11:05 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I would do
Code:
  @ManyToOne(fetch=LAZY) @JoinColumn(name="PK_A_ID")
  public A getA() { return a; }


and mappedBy on the other side because LAZY mean only a proxy hence getA().getId() will not load the object.

Otherwise I guess the following works

Code:
  @Column(name="PK_A_ID")
  public Long getAId() { return aId; }


...

  @OneToMany
  @JoinColumn(name = "PK_A_ID", insertable=false, updatable=false)
  public Set getBs() { return bs; }

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.