users@glassfish.java.net

JPA and SQL Server 2005's Identity Don't Play Nice

From: <glassfish_at_javadesktop.org>
Date: Sat, 08 Mar 2008 15:20:29 PST

I am hoping someone with a little more senior knowledge of JPASQL Server 2005 can point me in the right direction involving SQL Server 2005.

What I am discovering is JPA and SQL Server 2005 do not playing nice together when you use the IDENTITY capability for
a primary key. Let me explain. "...when using the IDENTITY as the generator type, the value for the identity field MAY NOT be
available BEFORE the entity data is saved in the database because typically it is generated when a record is committed."

My problem is this.

I have two separate entities using another entity, for example a car has a wheel and a truck has a wheel. I can go through the steps
of persisting the car and the wheel with no problem. My problem begins when I try to
find the wheel (by wheel description) for the truck. The query finds the wheel, but does not return the primary key for wheel.
Therefore, using the PK inside a query generates this error:

[code]
java.lang.IllegalArgumentException: An instance of a null PK has been incorrectly provided for this find operation.
[/code]


What has been my appraoch to get the entity to commit to the database is to use the following sequence for the Entity Manager.

[code]
entityManager.persist(wheel);
entityManager.flush();
[/code]


I have assumed to this point this action would persist my data because I have been able to retrieve the PK immidately after these steps.
However, multiple actions taking place within the same transaction, appears to render the persist and flush steps useless. My initial thought
is to wrap the wheel creation within a new transaction so when the method/transaction completes, the data would be in the database. This
has not worked.

I hope I have explained this clear for others to follow. I apologize in advance if I have not. Any suggestions are greatly appreciated.


Russ
[Message sent by forum member 'russ205' (russ205)]

http://forums.java.net/jive/thread.jspa?messageID=263051