Hi,
for my current client I have to evaluate an ORM-Tool appropriate to their requirements. Hibernate fits nearly all the requirements in a really smart way. Anyway, there is one problem I cannot solve:
My client is using DB2 User-Defined Types as the data type for
primary key columns. One of the ramifications of this is that DB2 requires explicit casts in sql statements that use these types of columns.
Unfortunately, Hibernate doesn"t generate the correct sql in these cases.
In my example for a selection I am using 2 tables with a simple association:
table VEHKIND:
VKND_OID (AUTOBOERSE.OID (user-defined type based on the underlying type BIGINT))
etc..
table VEHBODTYPE:
VBTY_OID (AUTOBOERSE.OID)
VKND_OID (AUTOBOERSE.OID (foreign key to table VEHKIND))
etc..
Hibernate's generated sql looks as follow:
select vehiclekin0_.VKND_OID as VKND_OID0_, vehiclekin0_.VKND_ID as VKND_ID0_, vehiclekin0_.VKND_LABEL as VKND_LABEL0_ from AUTOBOERSE.VEHKIND vehiclekin0_ where vehiclekin0_.VKND_OID=?
The following sql works:
select vehiclekin0_.VKND_OID as VKND_OID0_, vehiclekin0_.VKND_ID as VKND_ID0_, vehiclekin0_.VKND_LABEL as VKND_LABEL0_ from AUTOBOERSE.VEHKIND vehiclekin0_ where vehiclekin0_.VKND_OID=AUTOBOERSE.OID(CAST
(? AS BIGINT))
So what I need to know is, how I can tell Hibernate to generate the explicit cast according to the user-defined type.
I hope you can help.
If you need additional informations, let me know.
Best regards,
Michael Mermagen
Hibernate version:
2.1.6
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping schema="AUTOBOERSE" package="de.ccb.orm.model">
<class name="VehicleKind" table="VEHKIND">
<id name="oid" type="de.ccb.orm.hibernate.types.OidUserType" unsaved-value="null" >
<column name="VKND_OID" sql-type="AUTOBOERSE.OID" not-null="true"/>
<generator class="increment"/>
</id>
<property name="id">
<column name="VKND_ID" sql-type="AUTOBOERSE.ID" not-null="true"/>
</property>
<property name="label">
<column name="VKND_LABEL" not-null="true"/>
</property>
</class>
<class name="VehicleBodyType" table="VEHBODTYPE">
<id name="oid" type="de.ccb.orm.hibernate.types.OidUserType" unsaved-value="null" >
<column name="VBTY_OID" sql-type="AUTOBOERSE.OID" not-null="true"/>
<generator class="increment"/>
</id>
<property name="id">
<column name="VBTY_ID" sql-type="AUTOBOERSE.ID" not-null="true"/>
</property>
<property name="label">
<column name="VBTY_LABEL" not-null="true"/>
</property>
<property name="bit">
<column name="VBTY_BIT" not-null="true"/>
</property>
<many-to-one name="vehicleKind" class="VehicleKind" column="VKND_OID"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.currentSession();
Query q = session.createQuery("from de.ccb.orm.model.VehicleBodyType as vehicleBodyType");
List l = q.list();
HibernateUtil.closeSession();
Full stack trace of any exception that occurs:
[18.11.04 14:00:16:796 CET] 4f1c4f1c SystemOut O Hibernate: select vehiclebod0_.VBTY_OID as VBTY_OID, vehiclebod0_.VBTY_ID as VBTY_ID, vehiclebod0_.VBTY_LABEL as VBTY_LABEL, vehiclebod0_.VBTY_BIT as VBTY_BIT, vehiclebod0_.VKND_OID as VKND_OID from AUTOBOERSE.VEHBODTYPE vehiclebod0_
[18.11.04 14:00:17:078 CET] 4f1c4f1c SystemOut O Hibernate: select vehiclekin0_.VKND_OID as VKND_OID0_, vehiclekin0_.VKND_ID as VKND_ID0_, vehiclekin0_.VKND_LABEL as VKND_LABEL0_ from AUTOBOERSE.VEHKIND vehiclekin0_ where vehiclekin0_.VKND_OID=?
[18.11.04 14:00:17:218 CET] 4f1c4f1c JDBCException W net.sf.hibernate.util.JDBCExceptionReporter SQL Error: -432, SQLState: 42841
[18.11.04 14:00:17:234 CET] 4f1c4f1c JDBCException E net.sf.hibernate.util.JDBCExceptionReporter [SQL0432] Eine Parametermarkierung kann nicht die benutzerdefinierte Art OID haben.
[18.11.04 14:00:17:265 CET] 4f1c4f1c JDBCException W net.sf.hibernate.util.JDBCExceptionReporter SQL Error: -432, SQLState: 42841
[18.11.04 14:00:17:265 CET] 4f1c4f1c JDBCException E net.sf.hibernate.util.JDBCExceptionReporter [SQL0432] Eine Parametermarkierung kann nicht die benutzerdefinierte Art OID haben.
[18.11.04 14:00:17:265 CET] 4f1c4f1c JDBCException E net.sf.hibernate.util.JDBCExceptionReporter could not load: [de.ccb.orm.model.VehicleKind#de.ccb.orm.model.value.Oid@75687568]
[18.11.04 14:00:17:281 CET] 4f1c4f1c JDBCException E net.sf.hibernate.util.JDBCExceptionReporter TRAS0014I: Die folgende Ausnahmebedingung wurde protokolliert: java.sql.SQLException: [SQL0432] Eine Parametermarkierung kann nicht die benutzerdefinierte Art OID haben.
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java)
at java.sql.SQLException.<init>(SQLException.java:52)
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:388)
at com.ibm.as400.access.AS400JDBCStatement.commonPrepare(AS400JDBCStatement.java:1088)
at com.ibm.as400.access.AS400JDBCPreparedStatement.<init>(AS400JDBCPreparedStatement.java:208)
at com.ibm.as400.access.AS400JDBCConnection.prepareStatement(AS400JDBCConnection.java:1419)
at com.ibm.as400.access.AS400JDBCConnectionHandle.prepareStatement(AS400JDBCConnectionHandle.java:669)
at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.prepareStatement(WSJdbcConnection.java:1458)
at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.prepareStatement(WSJdbcConnection.java:1424)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:257)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:232)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:779)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:265)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1953)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:204)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2205)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:315)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at de.ccb.orm.hibernate.servlets.SearchVehicleAd.service(SearchVehicleAd.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:983)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:564)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:200)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:119)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:618)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:443)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java)
.
java.sql.SQLException: [SQL0432] Eine Parametermarkierung kann nicht die benutzerdefinierte Art OID haben.
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java)
at java.sql.SQLException.<init>(SQLException.java:52)
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:388)
at com.ibm.as400.access.AS400JDBCStatement.commonPrepare(AS400JDBCStatement.java:1088)
at com.ibm.as400.access.AS400JDBCPreparedStatement.<init>(AS400JDBCPreparedStatement.java:208)
at com.ibm.as400.access.AS400JDBCConnection.prepareStatement(AS400JDBCConnection.java:1419)
at com.ibm.as400.access.AS400JDBCConnectionHandle.prepareStatement(AS400JDBCConnectionHandle.java:669)
at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.prepareStatement(WSJdbcConnection.java:1458)
at com.ibm.ws.rsadapter.jdbc.WSJdbcConnection.prepareStatement(WSJdbcConnection.java:1424)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:257)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:232)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:65)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:779)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:265)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1953)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:204)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2205)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:315)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at de.ccb.orm.hibernate.servlets.SearchVehicleAd.service(SearchVehicleAd.java)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:983)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:564)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:200)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:119)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:618)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:443)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java)
Name and version of the database you are using:
DB2/400 V5R2 M0 + several Patches
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|