Hi. I have some experience with Hibernate, and have it all basically working (with a recent hibernate version), i.e. returning simple object-relation mappings, but my new many-to-one mappings are not working, and I don't understand why. Looking over my books and tutorials and such, they look ok to me.
Basically, I have a link table, with foreign keys in it pointing out to several "master" tables, the former being the "many"; the latter being the "one".
I can see Hibernate executing the select on the link table, but I never see it doing any kind of join to the master tables. I suspect this is why the link object (referral) looks ok, but the master table objects (such as person) contains null values in all attributes (private instance-level variables). The console log shows no error.
The significant sections of my code:
Link table object:
Code:
package com.eastridgesoftware.bestpeople.model;
public class Referral implements IXmlSerializable {
private static final long serialVersionUID = 1L;
private Long referralId;
private Long personId;
private Long professionId;
private Long friendFilterId;
private Person person;
private Profession profession;
private FriendFilter friendFilter;
@Override
public String toXml() {
String xml="<Referral>"
+"<ReferralId>"+referralId+"</ReferralId>"
+person.toXml()
+profession.toXml()
+friendFilter.toXml()
+"</Referral>";
return xml;
}
....plus the standard getter/setters for the above.
My link table object's hbm file:
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>
<class name="com.eastridgesoftware.bestpeople.model.Referral" table="referral">
<id name="referralId" column="referralId" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="friendFilterId" column="friendFilterId" type = "java.lang.Long"/>
<many-to-one name="person" class="com.eastridgesoftware.bestpeople.model.Person" column="personId" cascade="all"></many-to-one>
cascade="all"></many-to-one>
</class>
</hibernate-mapping>
One of my master table classes:
Code:
package com.eastridgesoftware.bestpeople.model;
public class Person implements IXmlSerializable {
private static final long serialVersionUID = 1L;
private Long personId;
private String userNm;
private String userPass;
private String lastNm;
private String firstNm;
private String nickNm;
private String phone;
private String emailAddress;
private String addressLine1;
private String addressLine2;
private String city;
private String stateCd;
private String zipCd;
private String countryCd;
@Override
public String toXml() {
String xml="<Person>"
+"<PersonId>"+personId+"</PersonId>"
+"<UserNm>"+userNm+"</UserNm>"
+"<UserPass>"+userPass+"</UserPass>"
+"<LastNm>"+lastNm+"</LastNm>"
+"<NickNm>"+nickNm+"</NickNm>"
+"<Phone>"+phone+"</Phone>"
+"<EmailAddress>"+emailAddress+"</EmailAddress>"
+"<AddressLine1>"+addressLine1+"</AddressLine1>"
+"<AddressLine2>"+addressLine2+"</AddressLine2>"
+"<City>"+city+"</City>"
+"<StateCd>"+stateCd+"</StateCd>"
+"<ZipCd>"+zipCd+"</ZipCd>"
+"<CountryCd>"+countryCd+"</CountryCd>"
+"</Person>";
return xml;
}
...plus again, the standard getter/setters for the above.
The hbm file for the master table class. Nothing exotic here:
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>
<class name="com.eastridgesoftware.bestpeople.model.Person" table="person">
<id name="personId" column="personId" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="userNm" column="userNm" type="java.lang.String"/>
<property name="userPass" column="userPass" type = "java.lang.String"/>
<property name="lastNm" column="lastNm" type = "java.lang.String"/>
<property name="firstNm" column="firstNm" type = "java.lang.String"/>
<property name="nickNm" column="nickNm" type = "java.lang.String"/>
<property name="phone" column="phone" type = "java.lang.String"/>
<property name="emailAddress" column="emailAddress" type = "java.lang.String"/>
<property name="addressLine1" column="addressLine1" type = "java.lang.String"/>
<property name="addressLine2" column="addressLine2" type = "java.lang.String"/>
<property name="city" column="city" type = "java.lang.String"/>
<property name="stateCd" column="stateCd" type = "java.lang.String"/>
<property name="zipCd" column="zipCd" type = "java.lang.String"/>
<property name="countryCd" column="countryCd" type = "java.lang.String"/>
</class>
</hibernate-mapping>
My hibernate config file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/MyDatabase</property>
<property name="connection.username">root</property>
<property name="connection.password">MyPassword</property>
<property name="connection.autocommit">true</property>
<!-- SQL Dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_contextclass">jta</property>
<!-- Echo all SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<mapping resource="com/eastridgesoftware/bestpeople/model/Referral.hbm.xml"/>
<mapping resource="com/eastridgesoftware/bestpeople/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My DAO class:
Code:
package com.eastridgesoftware.bestpeople.model;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.eastridgesoftware.bestpeople.model.Person;
import com.eastridgesoftware.bestpeople.model.Referral;
public class BestPeopleDao extends HibernateDaoSupport implements IBestPeopleDao {
@Override
public Collection<Person> getPerson(String userNm) {
Session session = HibernateUtil.getSession();
Criteria criteria = session.createCriteria(Person.class);
if(userNm != null){
criteria = criteria.add(Restrictions.eq("userNm",userNm));
}
return criteria.list();
}
@Override
public Collection<Referral> getAllReferrals() {
//Session session = HibernateUtil.getSession();
//Criteria criteria = session.createCriteria(FriendFilter.class);
return getHibernateTemplate().find("from Referral");
}
}
The console log:
Code:
07:34:42,586 INFO [STDOUT] DispatcherServlet with name 'spring-dispatcher' processing request for [/BestPeople/get-referral.htm]
07:35:11,441 INFO [STDOUT] Opening Hibernate Session
07:35:12,735 INFO [STDOUT] Hibernate: select referral0_.referralId as referralId1_, referral0_.friendFilterId as friendFi2_1_, referral0_.personId as personId1_, referral0_.professionId as professi4_1_ from referral referral0_
07:35:13,168 INFO [STDOUT] Eagerly flushing Hibernate session
07:35:13,197 INFO [STDOUT] Closing Hibernate Session
Why is this not working?
Ben Ethridge