Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3
Mapping documents:
Code:
<class name="no.song.b2b.dao.OrderDataDao" table="ordre">
<id name="ordreNummer" column="ordre_data_id" unsaved-value="-1">
<generator class="increment"/>
</id>
<discriminator column="data_type" type="string"/>
<property name="orderData" column="ordre_data" type="text"/>
<subclass name="no.song.b2b.domain.OrderDataImpl" discriminator-value="generic"/>
<subclass name="no.song.b2b.clients.jara.JaraOrderDataImpl" discriminator-value="jara"/>
</class>
<class name="no.song.b2b.dao.OrderProcessDao" table="ordre_prosess">
<id name="processId" column="prosess_id" unsaved-value="-1">
<generator class="increment"/>
</id>
<discriminator column="prosess_type" type="string"/>
<many-to-one name="orderData"
class="no.song.b2b.dao.OrderDataDao"
cascade="all"
column="ordre_data"
unique="true" />
<subclass name="no.song.b2b.dao.JaraOrderProcessDao">
<join table="ordre_prosess_jara">
<key column="prosess_id"/>
</join>
<subclass name="no.song.b2b.clients.jara.JaraOrderProcessImpl" discriminator-value="Jara_1">
<set name="transactions" lazy="true" inverse="true" cascade="all">
<key column="prosess_id"/>
<one-to-many class="no.song.b2b.dao.JaraTransactionDAO" />
</set>
</subclass>
</subclass>
</class>
<class name="no.song.b2b.dao.JaraTransactionDAO" table="jara_transaksjoner">
<id name="transactionId" column="transaksjon_id" unsaved-value="-1">
<generator class="increment"/>
</id>
<discriminator column="transaksjons_type" type="string"/>
<many-to-one name="orderProcess" cascade="all" column="prosess_id" class="no.song.b2b.dao.JaraOrderProcessDao">
</many-to-one>
<property name="conversationId" column="samtale_id"/>
<property name="stateMessage" column="status_melding"/>
<property name="state" column="status_kode"/>
<property name="executionOrder" column="exec_order"/>
<subclass name="no.song.b2b.clients.jara.transactions.GetCustomerByNumber" discriminator-value="customerByNumber"/>
<subclass name="no.song.b2b.clients.jara.transactions.GetCustomerBySearch" discriminator-value="customerBySearch"/>
<subclass name="no.song.b2b.clients.jara.transactions.GetAddressBySearch" discriminator-value="addressBySearch"/>
</class>
The generated SQL (show_sql=true):
select orderproce0_.prosess_id as prosess1_0_, orderproce0_.ordre_data as ordre3_1_0_, orderproce0_.prosess_type as prosess2_0_ from ordre_prosess orderproce0_ left outer join ordre_prosess_jara orderproce0_1_ on orderproce0_.prosess_id=orderproce0_1_.prosess_id where orderproce0_.prosess_id=?
stack-trace :Quote:
org.springframework.orm.hibernate3.HibernateSystemException: Exception occurred inside setter of no.song.b2b.clients.OrderProcessImpl.orderData; nested exception is org.hibernate.PropertyAccessException: Exception occurred inside setter of no.song.b2b.clients.OrderProcessImpl.orderData
org.hibernate.PropertyAccessException: Exception occurred inside setter of no.song.b2b.clients.OrderProcessImpl.orderData
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:51)
at org.hibernate.tuple.AbstractTuplizer.setPropertyValues(AbstractTuplizer.java:207)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:176)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2919)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:530)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:167)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:621)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:614)
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:389)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:383)
at org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:378)
at no.song.b2b.dao.B2bDao.findByPrimaryKey(B2bDao.java:24)
at test.testComponent.testAll(testComponent.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
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 org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
... 47 more
Caused by: java.lang.ClassCastException
at no.song.b2b.clients.OrderProcessImpl.setOrderData(OrderProcessImpl.java:40)
... 52 more
Failing set-method: (the property is an OrderDataImpl, a class implementing the interface OrderDataDao)Code:
public void setOrderData(OrderDataDao orderData) {
log.debug("Setting DAO-orderData "+orderData.getClass().getName());
this.orderData = (OrderDataImpl) orderData;
}
log from the code :Quote:
Setting DAO-orderData org.hibernate.proxy.HibernateProxy$$EnhancerByCGLIB$$dcfea0df
Obviously the problem is that Hibernate tries to set the property using a proxy, instead of instaciating the correct subclass (which in this particulare case is no.song.b2b.clients.jara.JaraOrderDataImpl)
The mapping works fine when I save my objects, but not when I load it back.
For the test I create one OrderProcess and one OrderData, assoicate them, and save it, and immideately try to load it back using the newly generated key :
Code:
OrderProcessDao dproc = new JaraOrderProcessImpl();
OrderDataDao ddata = new JaraOrderDataImpl();
ddata.setOrderData("test3");
dproc.setOrderData(ddata);
dao.saveOrUpdate(dproc);
log.debug("----------");
dao.findByPrimaryKey(OrderProcessDao.class,new Integer(dproc.getProcessId()));
log.debug("----------");
When checking the database (MySQL), the data looks fine..
How should the mepping look like to make it work both for saving and loading ?