-->
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: Unidirectional associations with join tables(many-to-one)
PostPosted: Wed Oct 24, 2012 1:40 am 
Newbie

Joined: Wed Oct 24, 2012 1:27 am
Posts: 3
Hi,

I am new for hibernate.I use unidirectional associations with join tables(many-to-one) according to Hibernate3.6 Reference Documentation below.When I query by Person,the generated sql always has a out left join on table 'PersonAddress'.Does it make sense?Is there a lazy way to only query table 'Person' when i do not use 'address' attribute in Person Object?

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</join>

</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>


Top
 Profile  
 
 Post subject: Re: Unidirectional associations with join tables(many-to-one)
PostPosted: Wed Oct 24, 2012 4:43 am 
Newbie

Joined: Wed Oct 24, 2012 3:47 am
Posts: 10
Location: Germany
Is there a certain reason why you use a join table on many-to-one association? A @JoinColumn in the Address would be sufficient. If not then you actually have a many-to-many association.

To answer your question: I don't know. But I guess that hibernate needs the foreign key to initialize the proxies in your "Person.address" property.

_________________
Regards,

Sebastian


Top
 Profile  
 
 Post subject: Re: Unidirectional associations with join tables(many-to-one)
PostPosted: Wed Oct 24, 2012 6:24 am 
Newbie

Joined: Wed Oct 24, 2012 1:27 am
Posts: 3
s.goetz wrote:
Is there a certain reason why you use a join table on many-to-one association? A @JoinColumn in the Address would be sufficient. If not then you actually have a many-to-many association.

To answer your question: I don't know. But I guess that hibernate needs the foreign key to initialize the proxies in your "Person.address" property.


Hi,

Thank you for your reply.
The cause is that I can not make a decision on database schema,the tables are already designed by SA.
Do you know what the necessary of hibernate initializing the proxy is?
The table 'Person' is so basic in our system that many functions will use it.
However,the attribute 'address' is not popular,I think the SQL is not supposed to having out join always.


Top
 Profile  
 
 Post subject: Re: Unidirectional associations with join tables(many-to-one)
PostPosted: Wed Oct 24, 2012 8:46 am 
Newbie

Joined: Wed Oct 24, 2012 3:47 am
Posts: 10
Location: Germany
heatonn1 wrote:
Do you know what the necessary of hibernate initializing the proxy is?


I am really no hibernate expert at all. Just started using it in may :-)
But even if an association (be it a to-one or a collection) is loaded lazily (default) the actual entity holding the association (the Person in your case) has a proxy set in place of the actual associated entities. This proxy holds the primary key of the target table so if the property of that entity is accessed later on with an open hibernate session it can be loaded with an extra query from the database or throw a LazyInitializationException if the session is closed.
As already mentioned this is why I am guessing that hibernate makes a join on your PersonAddress table. Maybe one of the experts here can correct me if I am wrong or explain more detailed.

If you do not want to load the addresses at all you could use projections in combination with a result transformer to specify the properties of your Person class that should be fetched from the database.

Code:
Criteria query = getSession().createCriteria(Person.class);

// some criterion to determine the record(s)
query.add(Restrictions.like("name", p_searchInput));

// projection list for property selection
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("name"), "name");
projectionList.add(Projections.property("persno"), "persno");
// any property of Person.class you want to load
query.setProjection(projectionList);

// result transformer to get results as Person instances
query.setResultTransformer(Transformers.aliasToBean(Person.class));

return query.list();


Regards

Sebastian

_________________
Regards,

Sebastian


Top
 Profile  
 
 Post subject: Re: Unidirectional associations with join tables(many-to-one)
PostPosted: Wed Oct 24, 2012 9:59 pm 
Newbie

Joined: Wed Oct 24, 2012 1:27 am
Posts: 3
Many thanks for your explaining about proxy and solution.
But it seems that many places which were already used such load() method all need to change.It is a large effort,it not my meaning.
I will use native SQL for my independent function.


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.