Hi,
I am trying to read objects from XML and persist into DB through hibernate. The below peice of code is able to extract the object but the session.save() method is throwing a ClassCastException because it is expecting the associated object in place of its id.
Please suggest how to fix this problem.
Code:
session = sessionFactory.openSession();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
tx = dom4jSession.beginTransaction();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(
"c:/xmloutput/productParameterTypes_input.xml"));
List list = document.selectNodes("//ProductParameterType");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object productParameterType = iter.next();
dom4jSession.save(ProductParameterType.class.getName(), productParameterType);
}
session.flush();
tx.commit();
session.close();
Following is the XML data that is being persisted:
Code:
<productParameterTypes>
<ProductParameterType>
<id>0</id>
<version>0</version>
<channel>0</channel>
<cpmScalingFactor>1.0</cpmScalingFactor>
<dataType>1</dataType>
<description>Measurement time for SELT</description>
<ioType>1</ioType>
<measuringUnit></measuringUnit>
<mibScalingFactor>1.0</mibScalingFactor>
<name>test</name>
<namedParameter>165</namedParameter>
<oid>1.3.6.1.4.1.193.72.300.40.1.3.4.1.1</oid>
<productDescriptor>10</productDescriptor>
<paramGroupIndex>0</paramGroupIndex>
</ProductParameterType>
</productParameterTypes>
Code:
Following is the stack trace of the exception when it is trying to associate NamedParameter object from <namedParameter>
java.lang.ClassCastException: java.lang.Integer
at org.hibernate.property.Dom4jAccessor$ElementGetter.get(Dom4jAccessor.java:192)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getVersion(AbstractEntityTuplizer.java:241)
at org.hibernate.persister.entity.AbstractEntityPersister.getVersion(AbstractEntityPersister.java:3601)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3319)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.def.DefaultReplicateEventListener.onReplicate(DefaultReplicateEventListener.java:122)
at org.hibernate.impl.SessionImpl.fireReplicate(SessionImpl.java:932)
at org.hibernate.impl.SessionImpl.replicate(SessionImpl.java:924)
at com.ericsson.cpm.xml.Cpm3rdPartyXmlTest.retrieveXMLDocument(Cpm3rdPartyXmlTest.java:46)
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:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)