openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bryan Noll (JIRA)" <>
Subject [jira] Commented: (OPENJPA-13) GenerationType.IDENTITY problem with MS SQL Server
Date Wed, 04 Oct 2006 20:16:20 GMT
    [ ] 
Bryan Noll commented on OPENJPA-13:

So... after looking into this issue for a bit, the problem is this:

Driver: jtds-1.2.jar
Column Type defined as: [ID] [int] IDENTITY (1, 1) NOT NULL

The ClassCastException is coming from this line of the generated code in the PersistenceCapable

id = (Integer)pcStateManager.replaceObjectField(this, i);

As a result of the previous method call, we eventually arrive at the 'org.apache.openjpa.jdbc.sql.DBDictionary.getGeneratedKey'
method, where there is this line of code...

Object key = rs.getObject(1);

When examining this 'key' object at runtime, its type is java.math.BigDecimal, so... the jtds
driver is returning this '[int] IDENTITY (1, 1)' type as a BigDecimal.

This problem does not occur when using a primitive 'int' as the type of the @Id mapping because
the org.apache.openjpa.jdbc.meta.strats.PrimitiveFieldStrategy.setAutoAssignedValue method
simply up-casts the 'autoInc' value to a Number (legally because it is a BigDecimal), and
then calls the 'intValue' method (all of this inside the 'case JavaTypes.INT' section of the
switch statement).  The corresponding class that has the setAutoAssignedValue method for the
non-primitive types is HandlerFieldStrategy.

The same problem occurs with MySQL when attempting to use a 'java.lang.Integer' as the type
of the @Id field, because the value is returned by the driver as a 'java.lang.Long' (driver:
mysql-connector-java-3.1.11.jar, colum type of 'int(11) - auto_increment').  This doesn't
seem as hinky, because using a Long as the @Id type seems more reasonable than having to use
a primitive or a BigDecimal.

I've thought of a couple of ways to go about resolving this, none of which I really like,
and am hoping one of the people more familiar with the code base can point me in the right

- Modify the bytecode enhancement so that it is try-catching for a ClassCastException, then
instead of casting, explicitly construct the wrapper type that you need, in this case Integer,
by casting to a Number, and then calling methods on that.

- Override the getGeneratedKey method in the SQLServerDictionary class to return a cast-safe
value.  Not good at all, because I don't see, as its modeled now, that this object is capable
of finding out at runtime in a dynamic way what exactly it needs to return.  As a hack, I
had it return an Integer, but that only worked because I knew the @Id field was mapped as
an Integer.

- In the StateManager, add methods that correspond to the replace<Primitive>Field (replaceIntField
gets called in the enhanced code when the @Id type is int), such as replaceIntegerWrapperField,
replaceLongWrapperField, etc... instead of just having replaceObjectField.

- Maybe someone can point out somewhere in the object hierarchy (thinking somewhere around
HandlerFieldStrategy.setAutoAssignedValue) where we can get to the information in the PersistenceCapable
implementor using the fieldName or index so we can find out what type it is supposed to be,
and construct it there from the BigDecimal (or whatever it is, Long with MySQL for instance).

- Don't change any code, and document somewhere what we know about what a person can and cannot
use for auto-increment @Id types for different databases.  This one seems good enough for

Thoughts... suggestions?

> GenerationType.IDENTITY problem with MS SQL Server
> --------------------------------------------------
>                 Key: OPENJPA-13
>                 URL:
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jpa
>         Environment: Microsoft SQL Server 2000
> Windows XP
> Java SE 1.5 
> OpenJPA - source downloaded today (Aug 14, 2006)
>            Reporter: Megan
>            Priority: Critical
> Cannot persist entity with identity column.   To reproduce, create a simple object with
identity column
> @Entity
> @Table(name="JpaType")
> public class JpaType implements Serializable
> {
>   @Id
>   @GeneratedValue(strategy=GenerationType.IDENTITY)
>   @Column(name="Id")
>   private Integer id = null;
>   @Column(name="Name")
>   private String name = null;
>   public Integer getId() { return id; }
>   public String getName() { return name;  }
>   public void setName(String name) { = name; }
> }
> create table JpaType (
>     Id int identity(1, 1) not null
>   , Name varchar(50) null
>   , constraint JpaType_PK primary key (Id)
> )
> JpaType jpa = new JpaType();
> jpa.setName("Test 1");
> em.persist(jpa);
> em.flush();
> It works OK if I remove identity column (and set ID myself).
> Stack trace
> <0|true|0.9.0> org.apache.openjpa.persistence.PersistenceException: java.math.BigDecimal
> 	at org.apache.openjpa.kernel.BrokerImpl.flush(
> 	at org.apache.openjpa.kernel.DelegatingBroker.flush(
> 	at org.apache.openjpa.persistence.EntityManagerImpl.flush(
> 	at mytest.domain.JpaTest.testJpa(
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> 	at java.lang.reflect.Method.invoke(
> 	at org.junit.internal.runners.TestMethodRunner.executeMethodBody(
> 	at org.junit.internal.runners.TestMethodRunner.runUnprotected(
> 	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(
> 	at org.junit.internal.runners.TestMethodRunner.runMethod(
> 	at
> 	at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(
> 	at
> 	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(
> 	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(
> 	at
> 	at
> 	at
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> 	at
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> Caused by: java.lang.ClassCastException: java.math.BigDecimal
> 	at mytest.domain.model.JpaType.pcReplaceField(
> 	at org.apache.openjpa.kernel.StateManagerImpl.replaceField(
> 	at org.apache.openjpa.kernel.StateManagerImpl.storeObjectField(
> 	at org.apache.openjpa.kernel.StateManagerImpl.storeField(
> 	at org.apache.openjpa.kernel.StateManagerImpl.storeField(
> 	at
> 	at org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy.setAutoAssignedValue(
> 	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(
> 	at org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(
> 	at org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flushPrimaryRow(
> 	at org.apache.openjpa.jdbc.kernel.OperationOrderUpdateManager.flush(
> 	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(
> 	at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(
> 	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(
> 	at org.apache.openjpa.kernel.DelegatingStoreManager.flush(
> 	at org.apache.openjpa.kernel.BrokerImpl.flush(
> 	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(
> 	at org.apache.openjpa.kernel.BrokerImpl.flush(
> 	... 23 more

This message is automatically generated by JIRA.
If you think it was sent incorrectly contact one of the administrators:
For more information on JIRA, see:


View raw message