Search code examples
javajpaderbyjboss6.x

JPA persist fails for GenerationType.IDENTITY


I have an old application that worked fine in JBOss 5, JPA and Derby. I am now porting it to JBoss 6, only to find that system is unable to insert the entity with error message:

Column 'ID' cannot accept a NULL value.

Where, ID is an identity column:

@Entity
public class Customer {
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    int id;
    ...
}

I checked the generated schema and it looks good:

CREATE TABLE "APP"."CUSTOMER" (
    "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    "EMAIL" VARCHAR(255), 
    "NAME" VARCHAR(255),
    "PHONE" VARCHAR(255));

You can look at the code of the application in the link below. Any help is appreciated.

https://docs.google.com/open?id=0B_lXBrNTL1s-R3NHb2hYZlJ1Znc

Raj


Solution

  • Well, I got things to work. Basically, when you use JPA to generate the schema in Derby, it creates the tables just fine, but then has problem inserting rows with identity column. The solution was not to generate schema from JPA. I manually created the schema and commented out the schema generation option.

    <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="ContactsPU" transaction-type="JTA">
        <jta-data-source>java:DerbyDS</jta-data-source>
        <class>com.webage.contacts.Customer</class>
        <!-- 
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
        </properties>
         -->
    </persistence-unit>
    </persistence>