-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with Hibernate not fetching set
PostPosted: Tue Mar 30, 2010 3:05 pm 
Newbie

Joined: Mon Aug 18, 2008 10:54 am
Posts: 11
Location: Devon, UK
I am having a problem with Hibernate not retrieving an inverse set.

I am using NetBeans 6.8 with the Hibernate Framework 3.2.5 and have used NetBeans to generate all of my config and POJO's from my mysql database model. I am using the InnoDB engine in mysql to enforce Foreign Key relationships.


I have three tables User, UserRoles and UserRole -

PK - Primary Key
FK - Foreign Key

User
-----
ID (PK)
Username
Password


UserRoles
-----------
ID (PK)
Username (FK -> User.Username)
Role (FK -> UserRole.Role)


UserRole
----------
Role
Description


I am issuing a Hibernate criteria query like this -

User u = (User)session.createCriteria(User.class).add(Restrictions.eq("username", username)).uniqueResult();

This returns the User and their details correctly. However when I call u.getUserRoleses(), then the Set returned is always empty. I was under the impression that Hibernate should lazy evaluate and give me the data set when I request it?

Is there some sort of problem with my mappings (below) or am I doing something wrong somehow?

My hibernate mappings files look like this -

User.hbm.xml
----------------
<hibernate-mapping>
<class catalog="accountablecare" name="uk.co.accountablecare.web.orm.generated.User" table="User">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="username" type="string">
<column length="25" name="Username" not-null="true" unique="true"/>
</property>
<property name="password" type="string">
<column name="Password" not-null="true"/>
</property>
<set inverse="true" name="userRoleses">
<key>
<column name="Username" not-null="true"/>
</key>
<one-to-many class="uk.co.accountablecare.web.orm.generated.UserRoles"/>
</set>
</class>
</hibernate-mapping>

UserRoles.hbm.xml
----------------------
<hibernate-mapping>
<class catalog="accountablecare" name="uk.co.accountablecare.web.orm.generated.UserRoles" table="UserRoles">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="identity"/>
</id>
<many-to-one class="uk.co.accountablecare.web.orm.generated.UserRole" fetch="select" name="userRole">
<column length="25" name="Role" not-null="true"/>
</many-to-one>
<many-to-one class="uk.co.accountablecare.web.orm.generated.User" fetch="select" name="user">
<column name="Username" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>


UserRole.hbm.xml
---------------------
<hibernate-mapping>
<class catalog="accountablecare" name="uk.co.accountablecare.web.orm.generated.UserRole" table="UserRole">
<id name="role" type="string">
<column length="25" name="Role"/>
<generator class="assigned"/>
</id>
<property name="description" type="string">
<column name="Description" not-null="true"/>
</property>
<set inverse="true" name="userRoleses">
<key>
<column length="25" name="Role" not-null="true"/>
</key>
<one-to-many class="uk.co.accountablecare.web.orm.generated.UserRoles"/>
</set>
</class>
</hibernate-mapping>


Thanks Adam.

_________________
Adam Retter


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate not fetching set
PostPosted: Wed Mar 31, 2010 3:14 pm 
Newbie

Joined: Mon Aug 18, 2008 10:54 am
Posts: 11
Location: Devon, UK
I managed to get an answer via the IRC channel from "sebersole" (thanks very much :-)), so I am reproducing the crux of it here in case anyone else has these problems in the future.

Basically the Mappings files that were generated for me by NetBeans were not correct in my case. Hibernate expects to map Foreign Keys onto Primary Keys by default unless otherwise told, in my case the 'Username' field of the 'User' table is not a Primary Key and so the mappings files need some additional information to be able to setup the relationship with the 'UserRoles' table, namely a property-ref attribute for the Username column.

Updated mappings files below -

User.hbm.xml
----------------
<hibernate-mapping>
<class catalog="accountablecare" name="uk.co.accountablecare.web.orm.generated.User" table="User">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="username" type="string">
<column length="25" name="Username" not-null="true" unique="true"/>
</property>
<property name="password" type="string">
<column name="Password" not-null="true"/>
</property>
<set inverse="true" name="userRoleses">
<key property-ref="username">
<column name="Username" not-null="true"/>
</key>
<one-to-many class="uk.co.accountablecare.web.orm.generated.UserRoles"/>
</set>
</class>
</hibernate-mapping>


And then also on the other end of the Mapping -

UserRoles.hbm.xml
----------------------
<hibernate-mapping>
<class catalog="accountablecare" name="uk.co.accountablecare.web.orm.generated.UserRoles" table="UserRoles">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="identity"/>
</id>
<many-to-one class="uk.co.accountablecare.web.orm.generated.UserRole" fetch="select" name="userRole">
<column length="25" name="Role" not-null="true"/>
</many-to-one>
<many-to-one class="uk.co.accountablecare.web.orm.generated.User" fetch="select" name="user" property-ref="username">
<column name="Username" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>

_________________
Adam Retter


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