|
I'm using Hibernate 3 and Spring Framework for this app.
Here is the setup:
I have two classes Facility_user and Facility_role. Every instance of Facility_user must have a security role id in Facility_role.
facility_user table:
emp_nbr varchar2 primary key
role_seq number
.....
facility_role table
role_seq number primary keu
access number
name varchar2
....
Facility_user class
...
public class Facility_user{
private String id;
private String first_name;
private String last_name;
private Facility_role role;
....
}
Facility_role class
...
public class Facility_role{
private Long id;
private String name;
....
}
I omitted a few irrelevant fields and attributes.
Here is my Hibernate mapping file
...
<hibernate-mapping>
<class name="Facility_user" table="facility_user">
<id column="emp_nbr" name="id">
<generator class="assigned"/>
</id>
<property lazy="false" name="first_name"/>
<property lazy="false" name="last_name"/>
<property lazy="false" name="created_dt" type="timestamp"/>
<property lazy="false" name="created_by"/>
<property lazy="false" name="updated_dt" type="timestamp"/>
<property lazy="false" name="updated_by"/>
<property lazy="false" name="home_page"/>
<many-to-one name="role" outer-join="false" class="Facility_role">
<column name="ROLE_SEQ"/>
</many-to-one>
</class>
</hibernate-mapping>
...
<hibernate-mapping>
<class name="Facility_role" table="facility_role">
<id column="ROLE_SEQ" name="id">
<generator class="sequence">
<param name="sequence">seqgen</param>
</generator>
</id>
<property lazy="false" name="name"/>
<property lazy="false" name="access_type" type="long"/>
</class>
</hibernate-mapping>
...
As you can see this is a typical many-to-one mapping.
The java code to retrieve Facility_user and Facility_role is as follows:
...
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List getGenericQueryFind(final Class aClass){
HibernateTemplate ht = new HibernateTemplate(this.sessionFactory);
List list = ht.loadAll(aClass);
return list;
}
And sessionFactory is set in the ApplicationContext.xml as
....
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/fedex/ground/facility/model/objects/Facility_role.hbm.xml</value>
<value>com/fedex/ground/facility/model/objects/Facility_user.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
All is well as I was able to retrieve all Facility_role records without problems in a call
getGenericQueryFind(Facility_role.class);
But the call of
getGenericQueryFind(Facility_user.class);
fails . The only diffference between the two calls is that the Facility_user class has Facility_role has a property whereas Facility_role is just an plain object mapped to the database.
I spent a lot of time debugging. The trace that looks interesting to me is that after it read the facility_user table --
...
2006-01-16 15:02:46,059 DEBUG [org.hibernate.loader.Loader] - total objects hydrated: 1
2006-01-16 15:02:46,059 DEBUG [org.hibernate.engine.TwoPhaseLoad] - resolving associations for [Facility_user#1998680]
2006-01-16 15:02:46,059 DEBUG [org.hibernate.event.def.DefaultLoadEventListener] - loading entity: [com.fedex.ground.facility.model.objects.Facility_role#2804]
2006-01-16 15:02:46,059 DEBUG [org.hibernate.event.def.DefaultLoadEventListener] - loading entity: [Facility_role#2804]
2006-01-16 15:02:46,059 DEBUG [org.hibernate.event.def.DefaultLoadEventListener] - creating new proxy for entity
2006-01-16 15:02:46,059 DEBUG [org.hibernate.jdbc.JDBCContext] - after autocommit
2006-01-16 15:02:46,059 DEBUG [org.hibernate.impl.SessionImpl] - after transaction completion
2006-01-16 15:02:46,059 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
2006-01-16 15:02:46,059 DEBUG [org.hibernate.impl.SessionImpl] - closing session
2006-01-16 15:02:46,059 DEBUG [org.hibernate.jdbc.ConnectionManager] - performing cleanup
2006-01-16 15:02:46,059 DEBUG [org.hibernate.jdbc.ConnectionManager] - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2006-01-16 15:02:46,059 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction completion
2006-01-16 15:02:46,059 DEBUG [org.hibernate.impl.SessionImpl] - after transaction completion
2006-01-16 15:02:46,069 DEBUG [org.hibernate.jdbc.ConnectionManager] - running Session.finalize()
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1789)
at com.sun.faces.config.ManagedBeanFactory.setPropertiesIntoBean(ManagedBeanFactory.java:558)
... 40 more
Caused by: java.lang.NullPointerException
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3120)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:812)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:782)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1375)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at org.springframework.orm.hibernate3.HibernateTemplate$5.doInHibernate(HibernateTemplate.java:519)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
at org.springframework.orm.hibernate3.HibernateTemplate.loadAll(HibernateTemplate.java:515)
What is the two phase load? Does Spring cuts off the session after loading Facility_user? I need help.
Thanks in advance.
|