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: Fail to load many-to-one relationship objects.
PostPosted: Mon Jan 16, 2006 5:30 pm 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
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.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 6:25 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I saw a NullPointer exception in your trace.
Could you verify that for every user there is a role. May be you are accessing a property of it somewhere but it is null.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 7:29 pm 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
Thanks for taking a look at it Sebastion. There is only one record in facility_user table. And none of the fields are null. Of course database field values not null means nothing about the java object properties. If there is something wrong, it would be the reference column, in this case, the ROLE_SEQ column in facility_user table that is mapped to object Facility_role. Can you tell from the mapping file I included what I did wrong in the many-to-one definition?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 8:04 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
your mapping looks fine, although I did not do a test.
I assume that the BeanUtils are doing
user.getRole().setAField and the role is null.
Please verify with a select on the database that there are no users with an empty role.
Or maybe you must initialize your Role at some point in the constructor.

I do not think that this is Hibernate here. You may also just run a test with a simple java application to verify that your mapping is fine.

You should do this anyway to test your Hibernate before you develop your dialogues. ;-)

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 9:07 pm 
Newbie

Joined: Wed Dec 21, 2005 4:03 pm
Posts: 11
Location: Pittsburgh
I did a somewhat big test by replacing the many-to-one property role in Facility_user with just a seq of Long. So I changed Facility_user.java, and the map. Effectively decouple the relationship between Facility_user and Facility_role. Now when I run it, there is no errors. So I can make a second call using the property seq of Facility_user to retrieve the corresponding Facility_role object.

So that narrowed down the problem to the many-to-one mapping, and confirmed that the seq column indeed has valid value that is retrieved by Hibernate.

I'll do another test with your suggestion of initializing the role property in facility_user to see if that helps any.

Looking closely to the nullpointer exception, it is on
org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)

I took a look at the souce code (googled) of AbstractEntityTuplizer.java. It seems Hibernate was unable to getProxy. What proxy are we talking about here? Do you know?


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.