-->
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: Association doesn't get retrieved.
PostPosted: Tue Dec 19, 2006 5:18 pm 
Newbie

Joined: Tue Oct 31, 2006 4:06 pm
Posts: 7
I have a user object with a many-to-many relationship table user_relationship. When I call user.getRelations(), it's null in the set. Any idea why? Is there something wrong in the "relations" mapping?

Thanks.



Here is the table DDL:

CREATE TABLE user_relationship
(
`id` VARCHAR(48) NOT NULL,
`user_id` VARCHAR(48) NOT NULL,
`friend_id` VARCHAR(48) NOT NULL,
`relationship_flag_id` VARCHAR(2) NOT NULL DEFAULT 'F',
PRIMARY KEY(`id`),
INDEX `User_Relationship_Composite_PK`(`user_id`, `friend_id`),
CONSTRAINT `FK_user_relationship_user` FOREIGN KEY `FK_user_relationship_user` (`user_id`)
REFERENCES `user` (`id`),
CONSTRAINT `FK_user_relationship_friend` FOREIGN KEY `FK_user_relationship_friend` (`friend_id`)
REFERENCES `user` (`id`),
CONSTRAINT `FK_user_relationship` FOREIGN KEY `FK_user_relationship` (`relationship_flag_id`)
REFERENCES `relationship` (`id`)
)




UserRelationship mapping:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="UserRelationship"
table="user_relationship"
lazy="true"
>
<cache usage="read-write" />

<id
name="id"
column="id"
type="java.lang.String"
unsaved-value="null"
>
<generator class="uuid.hex">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-UserRelationshipData.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<many-to-one
name="friend"
class="User"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="friend_id"
not-null="true"
/>

<many-to-one
name="owner"
class="User"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="user_id"
not-null="true"
/>

<many-to-one
name="relationship"
class="Relationship"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="relationship_flag_id"
not-null="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-UserRelationshipData.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>




UserRelationship class snippet:

public class UserRelationship implements Serializable
{
private String id;
private User owner;
private User friend;
private Relationship relationship;
}



User class mapping snippet:

<hibernate-mapping
>
<class
name="User"
table="user"
lazy="false"
>
<cache usage="read-write" />

<set name="relations" lazy="true" table="user_relationship">
<key column="user_id"/>
<composite-element class="UserRelationship">
<parent name="owner"/>
<many-to-one name="friend" class="User" column="friend_id" not-null="true"/>
<many-to-one name="relationship" class="Relationship" column="relationship_flag_id" not-null="true"/>
</composite-element>
</set>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 8:26 pm 
Newbie

Joined: Tue Oct 31, 2006 4:06 pm
Posts: 7
Bump. Anyone can help me out?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 4:51 am 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:13 pm
Posts: 32
Location: Spain (GU)
Hi,

You should use a lazy=false.

A brief explanation would be: If you use a lazy=true, then you must have a opened session.

Don't forget to rate,

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 2:46 pm 
Newbie

Joined: Tue Oct 31, 2006 4:06 pm
Posts: 7
It's not about the lazy fetch. I have the session open.

It seems that the association is completely ignored.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 4:11 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
NewToHibernateHere wrote:
It's not about the lazy fetch. I have the session open.

It seems that the association is completely ignored.



I've seen similar behavior if your association mapping definition isn't defined in your hibernate.cfg.xml file.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 6:48 am 
Newbie

Joined: Mon Nov 20, 2006 1:48 am
Posts: 7
Location: Hyderabad
Its a open session problem. its called Open Session view pattern. Its works in Transaction scoped level. Hence its better for u to use HQL query to get the parent object, then u'ld definetly get the collections rather than null.

pksiv wrote:
NewToHibernateHere wrote:
It's not about the lazy fetch. I have the session open.

It seems that the association is completely ignored.



I've seen similar behavior if your association mapping definition isn't defined in your hibernate.cfg.xml file.

_________________
Kalyan Tilak M


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.