Hi!
I am trying to make a cascading one-to-many association based on a foreign key which is not the Id. Has anyone already encountered this problem?
It works fine when I am inserting a child with a new parents - both persistents objects are inserted.
But it does not work when I am inserting a new child for an existing parent: I have to load the parent from its Id, and then when I am retrieving the list of its children to add a new one to it, the generated SQL is having something like:
where ForeignKey = Id
which can not work, because ForeignKey is a string whereas Id is an integer.
Is there any way to avoid this?
Regards,
HervE
Hibernate version: 3.05
Mapping documents:
Parent:
<hibernate-mapping
package="fr.mnh.handicap.modele">
<class
name="TypeCodification"
table="TYPE_CODIF"
schema="ADMINHANDICAP"
>
<id
name="idTypeCodification"
column="ID_TYP_CODIF"
type="integer">
<generator class="sequence">
<param name="sequence">SEQ_ID_TYP_CODIF</param>
</generator>
</id>
<version
name="auditVersion"
column="AUDIT_VERSION" />
<property
name="codeTypeCodification"
column="COD_TYP_CODIF"
type="string"
length="10"
not-null="true" />
<property
name="libelleTypeCodification"
column="LIB_TYP_COD"
type="string"
length="100"
not-null="true" />
<set
name="listeCodifications" lazy="true" cascade="save-update">
<key>
<column name="COD_TYP_CODIF" length="10" not-null="false" />
</key>
<one-to-many class="fr.mnh.handicap.modele.Codification" />
</set>
</class>
</hibernate-mapping>
Child:
<hibernate-mapping
package="fr.mnh.handicap.modele">
<class
name="Codification"
table="CODIF"
schema="ADMINHANDICAP"
>
<id
name="idCodification"
column="ID_CODIF"
type="integer">
<generator class="sequence">
<param name="sequence">SEQ_ID_CODIF</param>
</generator>
</id>
<version
name="auditVersion"
column="AUDIT_VERSION" />
<many-to-one
name="typeCodification"
class="TypeCodification"
cascade="save-update"
property-ref="codeTypeCodification">
<column name="COD_TYP_CODIF" length="10" not-null="true" />
</many-to-one>
<property
name="code"
column="COD"
type="string"
length="5"
not-null="true" />
<property
name="libelle"
column="LIB"
type="string"
length="100"
not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
TypeCodification monType = (TypeCodification) session.load(TypeCodification.class, new Integer(22));
Codification codif = new Codification();
codif.setCode("HvL2b");
codif.setLibelle("Test insertion Hervé");
codif.setTypeCodification(monType);
Set liste = monType.getListeCodifications();
liste.add(codif);
Full stack trace of any exception that occurs:
java.sql.SQLException: ORA-01722: invalid number
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:582)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1986)
at oracle.jdbc.ttc7.TTC7Protocol.fetch(TTC7Protocol.java:1253)
at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2532)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2850)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:609)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:537)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
at org.hibernate.loader.Loader.doQuery(Loader.java:391)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:176)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
at fr.mnh.handicap.test.TestCodifications.ajouterCodeHvL2b(TestCodifications.java:195)
at fr.mnh.handicap.test.Test.lancerCode(Test.java:24)
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 com.sun.faces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:126)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:267)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:381)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:75)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:534)
11:41:46,191 WARN [JDBCExceptionReporter] SQL Error: 1722, SQLState: 42000
11:41:46,191 ERROR [JDBCExceptionReporter] ORA-01722: invalid number
Name and version of the database you are using: Oracle 9.2.06
The generated SQL (show_sql=true):
INFO [STDOUT] Hibernate: select listecodif0_.COD_TYP_CODIF as COD3_1_, listecodif0_.ID_CODIF as ID1_1_, listecodif0_.ID_CODIF as ID1_0_, listecodif0_.AUDIT_VERSION as AUDIT2_12_0_, listecodif0_.COD_TYP_CODIF as COD3_12_0_, listecodif0_.COD as COD12_0_, listecodif0_.LIB as LIB12_0_, listecodif0_.NUM_ORDRE as NUM6_12_0_ from ADMINHANDICAP.CODIF listecodif0_ where listecodif0_.COD_TYP_CODIF=?
DEBUG [AbstractBatcher] preparing statement
DEBUG [IntegerType] binding '22' to parameter: 1
|