-->
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: HQL initialise problem
PostPosted: Tue Oct 07, 2008 3:38 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
Hibernate version: 3.2.6.ga

I have two classes, A and B.

Code:
public class A
{
  private Set<B> b = new Hashset<B>(0);

}
public class B
{
  ...
}

Mapped as following:

In A.hbm.mxl

<set name="b" lazy="true">
<key column="b_fk"/>
<one-to-many class="B"/>
</set>

In B.hbm.xml:

<many-to-one name="a" class="A" column="b_fk">
</many-to-one>

I have a hql query:

Select a
From A a inner join a.B b
where b.(lots of trivial conditions on 'b').


This gives me a list of A's in my dao. However, as the relationship bertween A and B is lazy; B is not initialised.

I can't iterate through the list and use Hibernate.initialise as this would ignore the conditions in the hql.
I know I could change the hql and get it to bring back on Object[] containing A and B but I wanted to get the B's through a.getB();

In my Dao:

Code:
        Query query =
                 aSession.getNamedQuery(myQueryName);
                 List<A> results = query.list();


Is there a way to initialise the B's from inside the hql, whilst still bringing only a List of A's back?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 5:07 am 
Newbie

Joined: Fri Aug 29, 2008 9:07 am
Posts: 16
Location: edinburgh/zielona gora
hi,
first of all I guess you should use MappedBy for manytoone relation - that will force hibernate to resolve that relation according to the first one. It should be done like that for bidirectional relations.

I use annotations but the example below should help you anyway:

class A
@OneToMany(mappedBy= "a_prop" ),
private Set<B> listofBs ;

class B
@ManyToOne
@JoinColumn(name = "a_id")
A a_prop;

It means that Set in class A is populated according to property a_prop in class B

I hope it helps


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 8:41 am 
Beginner
Beginner

Joined: Thu Jun 12, 2008 10:48 am
Posts: 24
Hi, thanks for the response.

I can't see the equivalent to "mappedBy" using mapping files. How do I do it?


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.