-->
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.  [ 2 posts ] 
Author Message
 Post subject: One to Many Mapping
PostPosted: Mon Sep 05, 2011 7:07 am 
Newbie

Joined: Sun Sep 04, 2011 3:54 am
Posts: 3
Hi, I have three Business Domain Classes mapped to theDB via Hibernate. They are,
Customer, Call and Action
One customer can make one or more calls and one call can have one or more actions.
The Customercall table has customer_id of Customer table as the foriegn key and the Action table has got call_id of the call table as the foriegn key.
The Business case is:
1. Add new customer - This went on fine
2. Add call made by customer - This went on fine
When doing the above business logic The Call List in the Customer domain class would be null and so I used the follwing query to fetch the collection and then added the call object to it and persisted it.
String hql="from Customer as customer left join fetch customer.calls where customer.customerId = ?";
3. Add action to be taken to the call made - Problem..
When doing this The Action List in the Call domain class would be null and so I used the follwing query to fetch the collection and then added the call object to it. This is from where I get the following exception..
Hibernate: select call0_.call_id as call1_2_0_, actions1_.action_id as action1_1_1_, call0_.time_and_date as time2_2_0_, call0_.notes as notes2_0_, actions1_.details as details1_1_, actions1_.required_by as required3_1_1_, actions1_.owning_user as owning4_1_1_, actions1_.complete as complete1_1_, actions1_.call_id as call6_0__, actions1_.action_id as action1_0__ from customercall call0_ left outer join action actions1_ on call0_.call_id=actions1_.call_id where call0_.call_id=?
Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: CGLIB Enhancement failed: com.hcl.domain.Customer; nested exception is org.hibernate.HibernateException: CGLIB Enhancement failed: com.hcl.domain.Customer
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:661)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:917)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
at com.hcl.dataaccess.ActionDaoHibernateImpl.getFullCallDetail(ActionDaoHibernateImpl.java:56)
at com.hcl.dataaccess.ActionDaoHibernateImpl.create(ActionDaoHibernateImpl.java:23)
at com.hcl.diary.DiaryManagementServiceProductionImpl.recordAction(DiaryManagementServiceProductionImpl.java:25)
at com.hcl.calls.CallHandlingServiceImpl.recordCall(CallHandlingServiceImpl.java:32)
at com.hcl.client.SimpleClientTest.main(SimpleClientTest.java:38)
Caused by: org.hibernate.HibernateException: CGLIB Enhancement failed: com.hcl.domain.Customer
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:96)
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.getProxy(CGLIBProxyFactory.java:49)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:379)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3460)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:275)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:196)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:882)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:850)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
at org.hibernate.type.EntityType.resolve(EntityType.java:379)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2205)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2102)
at org.hibernate.loader.Loader.list(Loader.java:2097)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1125)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:926)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
... 8 more
Caused by: java.lang.InstantiationException: com.hcl.domain.Customer$$EnhancerByCGLIB$$a6ba2fc1
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyInstance(CGLIBLazyInitializer.java:107)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:93)
... 32 more

The query that creates the problem is tring hql="from Call as call left join fetch call.actions where call.callId = ?";

Customer.hbm.xml

<hibernate-mapping>
<class name="com.hcl.domain.Customer" table="customer">
<id name="customerId" type="int" column="customer_id" >
<generator class="native"/>
</id>
<property name="companyName" column="company_name" />
<property name="email" column="email"/>
<property name="telephone" column="telephone"/>
<property name="notes" column="notes"/>
<list name="calls" cascade="all">
<key column="customer_id"/>
<list-index column="list_position"/>
<one-to-many class="com.hcl.domain.Call"/>
</list>
</class>
</hibernate-mapping>

Call.hbm.xml

<hibernate-mapping>
<class name="com.hcl.domain.Call" table="customercall">
<id name="callId" column="call_id">
<generator class="native"/>
</id>
<property name="timeAndDate" column="time_and_date"/>
<property name="notes" column="notes"/>
<one-to-one name="customer" cascade="all" constrained="true" foreign-key="customer_id" />
<set name="actions" cascade="all">
<key column="call_id"/>
<one-to-many class="com.hcl.domain.Action"/>
</set>
</class>
</hibernate-mapping>

Action.hbm.xml

<hibernate-mapping>
<class name="com.hcl.domain.Action" table="action">
<id name="actionId" column="action_id">
<generator class="native"/>
</id>
<property name="details" column="details"/>
<property name="requiredBy" column="required_by"/>
<property name="owningUser" column="owning_user"/>
<property name="completeInt" column="complete"/>
<one-to-one name="call" cascade="all" constrained="true" foreign-key="call_id"/>
</class>
</hibernate-mapping>

Can anyone please help me in spotting wat the problem is?
Thanks in advance, Anish


Top
 Profile  
 
 Post subject: Re: One to Many Mapping
PostPosted: Mon Sep 05, 2011 7:36 am 
Newbie

Joined: Sun Sep 04, 2011 3:54 am
Posts: 3
I set the property lazy="false" in the many-to-one tag and it resolved the issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.