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: