-->
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.  [ 3 posts ] 
Author Message
 Post subject: How-to on lazy one-to-one mapping?
PostPosted: Tue Oct 19, 2004 1:42 pm 
Beginner
Beginner

Joined: Tue Oct 19, 2004 11:04 am
Posts: 22
I have two classes and tables as defined below:

Code:
class A {
    B b;
    String name;
    getB();
    setB();
}

class B {
    A a;
    getA();
    getB();
}

table_a {
    id,
    name
}

table_b {
    id,
    a_id (unique)
}



I want to have a one-to-one relationship betwen A and B via the foreign key.

A<-->B
1 0..1

A can have 0 or 1 B, and B must have 1 A.

So the mappings would be thus:

Code:
<hibernate-mapping>
    <class name="A" table="table_a">
        <id name="id"  type="string">           
            <column name="id" sql-type="CHAR(36)" />
            <generator class="native"/>
        </id>

        <property name="name" type="string">
            <column name="name"/>
        </property>
       
        <one-to-one name="b" class="B" property-ref="a" cascade="all"/>
    </class>
<hibernate-mapping>

<hibernate-mapping>
    <class name="B" table="table_b">
        <id name="id"  type="string">           
            <column name="id" sql-type="CHAR(36)" />
            <generator class="native"/>
        </id>

        <many-to-one name="a" class="A" column="a_id" unique="true" cascade="none" />       
       
    </class>   
</hibernate-mapping>


This works perfectly. My question is, however, 'How do I change this mapping so that when querying instances of A, the B instances are NOT also queried?'. i.e. I want to be able to retrieve instances of A via a Session.get() without having the B members returned. (I want to get the Bs later...)

The retrieval code looks like this:
Code:
Session session  = sessionFactory.openSession();
trans = session.beginTransaction();
A a = (A)session.get(A.class, aId);
trans.commit();
session.close();

if (a.getB() != null) {
    System.out.println("This is bad");
}


I've tried using the lazy="true" attribute of class on both classes for proxying to no avail. I've also tried using various combinations of the outer-join attributes on the associations. Whenever I get A, I always end up getting B as well. In my case, B is a very heavyweight object that only should be retrieved when needed, so this is important (at least to me).

I'm using Hibernate 2.1.2.


Top
 Profile  
 
 Post subject: Re: How-to on lazy one-to-one mapping?
PostPosted: Tue Oct 19, 2004 2:07 pm 
Beginner
Beginner

Joined: Tue Oct 19, 2004 11:04 am
Posts: 22
jwisard wrote:

This works perfectly. My question is, however, 'How do I change this mapping so that when querying instances of A, the B instances are NOT also queried?'. i.e. I want to be able to retrieve instances of A via a Session.get() without having the B members returned. (I want to get the Bs later...)


...or, as an alternative, is there a straight-forward Hibernate-centric way to query A without B, using HQL or the Criteria interface?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 19, 2004 4:05 pm 
Newbie

Joined: Fri Aug 27, 2004 5:36 pm
Posts: 16
Location: Pittsburgh
The one way to do is declate one-to-one mapped class it self as lazy.

<class name="OneToOneRelatio" lazy="true">


Did you take a look at this ?

http://www.hibernate.org/162.html

_________________
--------------------------------------------
Vish
Help others too


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