-->
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.  [ 6 posts ] 
Author Message
 Post subject: createQuery load object where associated relation value=?
PostPosted: Thu Mar 30, 2006 5:54 am 
Newbie

Joined: Wed Mar 15, 2006 10:36 am
Posts: 14
Hi all,

I am a sort of beginner to hibernatre,
and using only XML-Mapping in hibernate mapping file.

If two enties are assciated with each other with one-to-one or one-to-many or many-to-one or many-to-many relationship, eg Parent assciated with children, so I want to load parent where children_id=?, or load the children object where parent_id=?.

To be more specific

I have a Product-User relationship defined as Many-to-Many, and I want to load product which has User as userId=?

Mapping for this
<bag name="productsUsers" table="ProductsUsers" node=".">
<key column="productId" />
<many-to-many entity-name="User" embed-xml="false"
column="userId" node="userId" />
</bag>


or I want to load Product where Bug is bugId=?

mapping for this is:

<bag name="bugsProducts" cascade="all" inverse="true"
node=".">
<key column="productId" />
<one-to-many entity-name="Bug" embed-xml="false"
node="bugId" />
</bag>



I hope I have made my question clear to you..

So is there any othere way to do it, with just specifying entity and relationship value in query.


Thanks in advance..

_________________
Gagan Jain


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 10:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I presume that your problem is only when the relationship is unidirectional. Bidirectional is easy. For unidirectional in HQL, use
Code:
select p
from Product p
join p.Bugs b
where b.id = ?
For Criteria, the model is
Code:
Criteria crit = sess.createCriteria(Product.class);
crit.addAlias("Bugs", "b");
crit.add(Restrictions.eq("b.id", bugId);
return (List<Product>) crit.list();


Top
 Profile  
 
 Post subject: Loading entity on relation value
PostPosted: Fri Mar 31, 2006 1:04 am 
Newbie

Joined: Wed Mar 15, 2006 10:36 am
Posts: 14
Hi tenwit

The complete XML look like this.


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="Bug" table="Bug" node="bug">
<id name="bugId" column="bugId" node="bugId"
type="java.lang.String">
<generator class="uuid" />
</id>
<version name="version" column="VERSION" node="version" />
<property name="bugInfo" column="bugInfo" node="bugInfo"
type="java.lang.String" length="80" not-null="true" />

<bag name="bugUsers" node="." cascade="all" inverse="true">
<key column="bugId" />
<one-to-many entity-name="User" embed-xml="false"
node="userId" />
</bag>

<many-to-one name="productId" column="productId"
node="productId" embed-xml="false" entity-name="Product" />

</class>

<class entity-name="Product" table="Product" node="product">
<id name="productId" column="productId" node="productId"
type="java.lang.String">
<generator class="uuid" />
</id>
<version name="version" column="VERSION" node="version" />
<property name="productInfo" column="PRODUCT_INFO"
node="productInfo" type="java.lang.String" length="80"
not-null="true" />

<bag name="bugsProducts" cascade="all" inverse="true"
node=".">
<key column="productId" />
<one-to-many entity-name="Bug" embed-xml="false"
node="bugId" />
</bag>

<bag name="productsUsers" table="ProductsUsers" node=".">
<key column="productId" />
<many-to-many entity-name="User" embed-xml="false"
column="userId" node="userId" />
</bag>
</class>

<class entity-name="User" table="USERS" node="user">

<id name="userId" column="userId" node="userId"
type="java.lang.String">
<generator class="uuid" />
</id>

<version name="version" column="VERSION" node="version" />

<property name="email" column="EMAIL" node="email"
type="java.lang.String" length="40" />

<many-to-one name="manager" node="manager" column="managerId"
entity-name="User" embed-xml="false" update="true" />

<many-to-one name="bugId" column="bugId" node="bugId"
embed-xml="false" entity-name="Bug" />

<bag name="productsUsers" node="." table="ProductsUsers" lazy="true"
inverse="true">
<key column="userId" />
<many-to-many entity-name="Product" embed-xml="false"
column="productId" node="productId" />
</bag>

<one-to-one name="userDetails" node="userDetails"
entity-name="UserDetail" embed-xml="false" />
</class>

<class entity-name="UserDetail" table="USER_DETAILS"
node="userDetail">
<id name="userId" column="userId" node="userId"
type="java.lang.String">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<version name="version" column="VERSION" node="version" />

<one-to-one name="user" node="." entity-name="User"
constrained="true" />

<property name="name" column="NAME" node="name"
type="java.lang.String" />
<property name="address" column="ADDRESS" node="address"
type="java.lang.String" />
<property name="phone" column="PHONE" node="phone"
type="java.lang.String" />
</class>

</hibernate-mapping>




My relation is bi-directional, The problem I am facing is, I can't do p.Bug as you said, because Bug is not the property or component of product. Correct me if I am wrong.

Thanks for the reply..

_________________
Gagan Jain


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 1:26 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The HQL is:
Code:
select p
from Product p
join p.BugsProducts bp
where bp.id = ?
You should be able to figure out the equivalent Criteria from my earlier example.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 1:58 am 
Newbie

Joined: Wed Mar 15, 2006 10:36 am
Posts: 14
Hi tenwit,
It works to some extent, but not exactly how i want.
I am retrieving Product as a Map by opening session in Map mode.
When I do this way, I retrieved a List having objects of Object class, not Map.

When I saw the query, it is also retrieving Bug attributes, How I can avoid that.
Regarding writing criteria, as I earlier mention, I am using only XML mapping, no POJO classes, so there is no Product.class as such. I am not sure that I can use entity_name.class for this.

Please correct me If I am wrong.

Thanks

_________________
Gagan Jain


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 2:22 am 
Newbie

Joined: Wed Mar 15, 2006 10:36 am
Posts: 14
Hi tenwit,

My problem is solved..
Thanks a lot..

_________________
Gagan Jain


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