-->
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: Hibernate not trying to fetch a set.
PostPosted: Wed Mar 04, 2009 6:38 pm 
Newbie

Joined: Wed Mar 04, 2009 6:07 pm
Posts: 2
Location: Emory University
I have a many-to-many mapping between classes named User and Data. My code successfully retrieves a User object from the database. The next thing it tries to do is retrieve a set of Data objects associated with the User object. It calls the User object's getData method which returns a PeristentSet, as it should. The code then calls the PersistentSet's size method, which incorrectly returns zero.

Hibernate never issues a select or any other SQL to try to fetch the contents of the set. I must be doing something stupid! Here is the code that is calling this:
Code:
        List<User> userList = userManager.findByUserId(userId3);
        assertEquals("add new,new single user creation", 1, userList.size());
        User user3 = userList.get(0);
        assertEquals("add new,new user ID", userId3, user3.getUserId());
        myLogger.debug("Getting data for new,new user");
        Set<Data> dataSet = user3.getData();
        assertEquals("add new,new data count", 2, dataSet.size());


I've tried using an iterator to access the set, but still hibernate did not issue a select to get the contents of the set. What could I be doing that would make hibernate not issue any SQL or throw an exception?

Hibernate version: 3.3.1

Mapping documents:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="edu.emory.cci.security.auth.User" table="user" catalog="auth">
<id name="key" type="java.lang.Long">
<column name="user_key" />
<generator class="identity" />
</id>
<property name="userId" type="java.lang.String" unique="true">
<column name="user_id" length="200" not-null="true">
<comment>The user's user ID.</comment>
</column>
</property>
<property name="name" type="java.lang.String">
<column name="user_name" length="80">
<comment>The user's name.</comment>
</column>
</property>
<set name="data" lazy="true" table="data_user">
<key column="user_key" />
<many-to-many column="data_key" class="edu.emory.cci.security.auth.Data" />
</set>
</class>
</hibernate-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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class catalog="auth" name="edu.emory.cci.security.auth.Data" table="data">
<id name="key" type="java.lang.Long">
<column name="data_key"/>
<generator class="identity"/>
</id>
<property generated="never" lazy="false" name="dataId" type="java.lang.String">
<column length="300" name="data_id" not-null="true">
<comment>The data id</comment>
</column>
</property>
<property generated="never" lazy="false" name="isDeleted" type="java.lang.String" insert="false">
<column length="1" name="is_deleted" not-null="true">
<comment>If T, the data_id is deleted. No physical delete is done.</comment>
</column>
</property>
<many-to-one lazy="proxy" name="submitter" class="edu.emory.cci.security.auth.User">
<column name="submitter">
<comment>User ID of submitter or deleter</comment>
</column>
</many-to-one>
<set name="users" inverse="true" table="data_user">
<key column="data_key" />
<many-to-many column="user_key" class="edu.emory.cci.security.auth.User"/>
</set>
</class>
</hibernate-mapping>

Name and version of the database you are using: MySQL 5.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:
1046 [main] DEBUG edu.emory.cci.security.auth.UserDAO - finding User instance with property: userId, value: taki557
1046 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
1046 [main] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
1046 [main] DEBUG org.hibernate.SQL -
/*
from
User as model
where
model.userId= ? */ select
user0_.user_key as user1_0_,
user0_.user_id as user2_0_,
user0_.user_name as user3_0_
from
auth.user user0_
where
user0_.user_id=?
Hibernate:
/*
from
User as model
where
model.userId= ? */ select
user0_.user_key as user1_0_,
user0_.user_id as user2_0_,
user0_.user_name as user3_0_
from
auth.user user0_
where
user0_.user_id=?
1046 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
1046 [main] DEBUG org.hibernate.loader.Loader - result row: EntityKey[edu.emory.cci.security.auth.User#3]
1046 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
1046 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
1046 [main] DEBUG org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
1046 [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
1046 [main] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
1046 [main] DEBUG edu.emory.cci.security.auth.AuthorizationTest - Getting data for new,new user

Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 8:51 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
Try setting lazy to "false", for the set.

LinHib.


Top
 Profile  
 
 Post subject: Lazy does not make a difference
PostPosted: Wed Mar 04, 2009 9:53 pm 
Newbie

Joined: Wed Mar 04, 2009 6:07 pm
Posts: 2
Location: Emory University
I've tried with lazy set to true or false. It made do difference. Hibernat did not issue a select to get the contents of the set.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 2:44 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
The only suspicious thing that I see is userManager.findByUserId(userId3). Change the test-case in the way that the 'user3' is created via session.load(User.class, userId3) and check the result. I believe the test-case is passed then.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 12:45 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
The set in User maps to "data_user", where as the Data class maps to "data" table. Is this intentional?

LinHib.


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.