Problem
I want to use one-to-many mapping to return a set. In User HBM, I want to return a cashSet in a user object. The abstract is
Code:
<!-- Set and One-to-Many Mapping to Cash -->
<set name="cashList" inverse="true">
<key column="userID"/>
<one-to-many class="com.cyproj.pm.db.Cash"/>
</set>
<!-- End of One-to-Many -->
The database is set correct and working. When I connect using debug mode of Netbean, I can see that the cashList and all other set return null when there is value in the database. The other values return successfully, for example, one-to-one mapping do return valid value. My log and hibernate log did not show any error too.
What had I gone wrong or missed out? I am new to hibernate. I had also searched and tried the result in Google, but to no help... Please help me.
I had provided my finance.hbm.xml, user.hbm.xml and the hibernate session code. Please point out any error or any pointer that I should take note. Thanks
Hibernate version: 3.1 Mapping documents:User HBM
Code:
<hibernate-mapping package="com.cyproj.pm.db">
<!-- Users Class -->
<class name="User" table="User" >
<id name="id" type="int" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="loginID" type="string" unique="true" not-null="true"/>
<property name="password" type="string" not-null="true"/>
<property name="question" type="string" not-null="true"/>
<property name="answer" type="string" not-null="true"/>
<!-- One-to-One Mapping to UserInfo -->
<one-to-one name="userInfo" class="com.cyproj.pm.db.UserInfo" cascade="all" />
<!-- End of One-to-One Mapping -->
<!-- One-to-One Mapping to UserInfo -->
<one-to-one name="userAccessRight" class="com.cyproj.pm.db.UserAccessRight" cascade="all" />
<!-- End of One-to-One Mapping -->
<!-- Set and One-to-Many Mapping to Contacts -->
<set name="contactList" inverse="true">
<key column="userID"/>
<one-to-many class="com.cyproj.pm.db.Contact" />
</set>
<!-- End of One-to-Many -->
<!-- Set and One-to-Many Mapping to Cash -->
<set name="cashList" inverse="true">
<key column="userID"/>
<one-to-many class="com.cyproj.pm.db.Cash"/>
</set>
<!-- End of One-to-Many -->
<!-- Set and One-to-Many Mapping to Credit Card -->
<set name="creditCardList" inverse="true">
<key column="userID"/>
<one-to-many class="com.cyproj.pm.db.CreditCard"/>
</set>
<!-- End of One-to-Many -->
</class>
<!-- End of User Class -->
</hibernate-mapping >
Finance HBM
Code:
<?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 package="com.cyproj.pm.db">
<!-- Finance Class -->
<class name="Finance" table="finance" >
<id name="id" type="int" unsaved-value="0" column="Finance_ID">
<generator class="identity"/>
</id>
<property name="date" type="date"/>
<property name="information" type="string"/>
<property name="amount" type="double"/>
<!-- Many-to-One Mapping User-->
<many-to-one name="user" class="com.cyproj.pm.db.User" column="userID" foreign-key="FK_FN_USER"/>
<!-- End of Many-to-One Mapping -->
<!-- Joined-Subclass to Credit Card Finance-->
<joined-subclass name="CreditCard" table="creditcard">
<key column="Finance_ID" not-null="true"/>
<property name="CreditCardNumber" type="integer"/>
<property name="isBillSettled" type="boolean"/>
</joined-subclass>
<!-- End of Joined-Subclass -->
<!-- Joined-Subclass to Cash Finance -->
<joined-subclass name="Cash" table="cash">
<key column="Finance_ID" not-null="true"/>
<property name="isSaving" type="boolean"/>
</joined-subclass>
<!-- End of Joined-Subclass -->
</class>
<!-- End of Finance Class -->
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public User getUserByUsername(String username){
Session session = HibernateUtil.getHbmSession();
Transaction txn = null;
User user = null;
try{
txn = session.beginTransaction();
Query query = session.createQuery("SELECT userObj FROM User AS userObj WHERE userObj.loginID LIKE '"+username+"'");
Iterator it = query.iterate();
user = (it.hasNext()) ? (User)it.next() : null;
txn.commit();
}catch(Exception e){
if(txn != null)
txn.rollback();
log.error("Hibernate error (getUserByUsername) - " + e);
}finally{
HibernateUtil.closeHbmSession();
}
return user;
}
Name and version of the database you are using: MYSQL