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: half bidirectional
PostPosted: Tue Nov 30, 2004 6:23 pm 
Newbie

Joined: Mon Nov 17, 2003 10:53 am
Posts: 15
hi,

i want to have a sort of half bidirectional
association. that is one end of the association
should be in charge of managing the association
but i want to be able to retrieve the other
end of the association from the db and navigate
back to the managing association.
in principle this is possible with a bit of
code...but i would like to declare that via
mapping docs.

ok lets have an example
Code:
public class Session
{
  public String getId(){...}
  public void setId(String _id){...}
  public List getTransactions(){...}
  public void setTransactions(List _val){...}
}

public class Transaction
{
  public String getId(){...}
  public void setId(String _id){...}

  public Session getParent(){...}
  public void setParent(Session _ses){...}
}

so basically this is a parent/child association
between session and transactions. simple
enough!

Mapping documents:
Code:
<hibernate-mapping>
  <class name="Session"
         table="SESSION"
         schema="SCHEMA" >
    <id name="id" column="ID" type="string" >
       <generator class="uuid.hex">
       </generator>
    </id>

    <list name="transactions"
          schema="SCHEMA"
          table="TX"
          lazy="false"
          cascade="all"
          inverse="false">
      <key column="SESSION_ID"/>
      <index column="POS"/>
      <one-to-many class="Transaction"/>
    </list>
  </class>
</hibernate-mapping>

<hibernate-mapping>
  <class name="Transaction"
         table="TX"
         schema="SCHEMA">
    <id name="id" column="ID">
      <generator class="uuid.hex">
      </generator>
    </id>

    <many-to-one name="session"             
                 column="SESSION_ID"/>

  </class>
</hibernate-mapping>


now this will work fine for creating sessions
and adding transactions to them (which is what
i want to do most of the time)
Code:
Session source = new Session();
source.getTransactions().add(new Transaction());
source.getTransactions().add(new Transaction());

save(source);
close();

String id = source.getId();

Session target = (Session) load(id);
assertEquals(2, target.getTransactions().size());

like i said up to here everything works fine
and there are no problems. but want i want
to do is something like this.
Code:
Session sess1 = new Session();
Ttansaction tx = new Transaction();
sess1.getTransactions().add(tx);
save(sess1);
close();

String id = tx.getId();

Transaction target = (Transaction) load(id);
Session sess2 = target.getSession();
assertNotNull(sess2);

now this fails. of course as the association
has not be defined to be directional. now
i dont want to make it bidirectional as
transactions should not be able to manage
to which session they belong to *but* i would
like to be able to navigate from transaction
to session *without* writing an explicit
load within the transaction class.

any ideas, any hints. or is this simply not
possible via mappings.

thanks

ciao robertj


Top
 Profile  
 
 Post subject: Retrieving parent from a one-to-many (unidirectional) mappin
PostPosted: Sat Jan 29, 2005 2:08 am 
Newbie

Joined: Tue Jan 11, 2005 1:01 am
Posts: 3
Hi,
I noticed this post quite some time ago and I had a similar question...

I want to retrieve the parent id from a one-to-many relationship (unidirectional)... I really don't want to make it bi-directional (because Parent object is really big and it will waste precious bandwidth recreating the parent just for getting the id).

A lot of people have said, 'read the manual'... and the truth is I have.. many times over.. but there is nothing listed on how to query the id out of a child..

Does anyone please have a direct link or a simple generic segment of hql code to how to retrieve the parent id from a child?

Thanks,
Saad


Top
 Profile  
 
 Post subject: Re: Retrieving parent from a one-to-many (unidirectional) ma
PostPosted: Sun Jan 30, 2005 11:07 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
SimFox3 wrote:
Hi,
I noticed this post quite some time ago and I had a similar question...

I want to retrieve the parent id from a one-to-many relationship (unidirectional)... I really don't want to make it bi-directional (because Parent object is really big and it will waste precious bandwidth recreating the parent just for getting the id).

A lot of people have said, 'read the manual'... and the truth is I have.. many times over.. but there is nothing listed on how to query the id out of a child..

Does anyone please have a direct link or a simple generic segment of hql code to how to retrieve the parent id from a child?

Thanks,
Saad


Hint: In HQL, expressions that refer to the identifier property (usually 'id') do not cause the entity to be retrieved. For example, child.parent.id would not force the parent to be retrieved.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 30, 2005 11:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
I really don't want to make it bi-directional (because Parent object is really big and it will waste precious bandwidth recreating the parent just for getting the id).


Hibernate never needs to retrieve an object just to get its id.


Top
 Profile  
 
 Post subject: Yipee do da
PostPosted: Tue Feb 01, 2005 1:43 am 
Newbie

Joined: Tue Jan 11, 2005 1:01 am
Posts: 3
Hi Guys,
Thanks for your inputs.. I guess you are right.. but still I didn't see the reason why you would need to store the parent.. I did a lot of testing and fooling around I found the answer.. What makes me sad was that fact that answer was semi-there in Chapter 11 on HQL..

Anyway this is the general form to retrieve id of parent from child in a one-to-many relationship.. (unidirectional):

Code:
Integer parentId = (Integer)s.find("select p.id from Parent as p where ? in elements(p.childs)",child,Hibernate.entity(Child.class)).get(0);


Saad.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.