Announcement Announcement Module
Collapse
No announcement yet.
Custom JPA Id (DB2): IdentifierGenerationException or DataIntegrityViolationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom JPA Id (DB2): IdentifierGenerationException or DataIntegrityViolationException

    Hello:

    I'm trying to use Roo for a new application that will use IBM DB2 as database.

    > We need to define custom IDs for each table or the SQL sentences will not work:

    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query

    select
    classname_.id as id1_,
    classname_.column1 as col2_1_,
    classname_.column2 as col3_1_
    from
    OWNER.txx classname_ fetch first 10 rows only


    There is no id column in the table TXX.

    We have two options:
    • To annotate the Java class using @javax.persistence.Id

    First we need to provide settter/getter in that class, because:

    User provided @javax.persistence.Id field but failed to provide a public 'get[attribute]' method in '[class]'

    Once it's done, we have the next problem:

    javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): package.MyClass

    I see some similar in http://forum.springsource.org/showthread.php?t=45541, but since the code is created by Roo, I cannot choose between save() or saveAndUpdate() and the solution doesn't fit for me.
    • To annotate the Java class using @RooEntity(identifierColumn="COLUMN_CODE", identifierField="code", identifierType=String.class)

    The attribute is erased from MyClass and Roo creates the appropriate content in MyClass_Roo_Entity:

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "COLUMN_CODE")
    private String MyClass.code;

    public String MyClass.getCode() {
    return this.code;
    }

    public void MyClass.setCode(String id) {
    this.code = id;
    }


    Roo deletes the getters/setters from MyClass_Roo_JavaBean as well.

    However, the test fails again:

    Tests run: 7, Failures: 0, Errors: 1, Skipped: 0
    testPersist[..] ERROR!


    org.springframework.dao.DataIntegrityViolationExce ption: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [MyClass]

    Caused by: com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -407, SQLSTATE: 23502, SQLERRMC: COLUMN_CODE


    That means:
    23502 An insert or update value is null, but the column cannot contain null values. -407

    And it's because the object to be persisted has the code to null (and it seems to be mandatory according the Asserts) which is not allowed by the definition of our table.

    @Test
    @Transactional
    public void Tgw00AplicacionIntegrationTest.testPersist() {
    org.junit.Assert.assertNotNull("Data on demand for 'MyClass' failed to initialize correctly", dod.getRandomMyClass());
    package.MyClass obj = dod.getNewTransientMyClass(Integer.MAX_VALUE);
    org.junit.Assert.assertNotNull("Data on demand for 'MyClass' failed to provide a new transient entity", obj);
    org.junit.Assert.assertNull("Expected 'MyClass' identifier to be null", obj.getCode());
    obj.persist();
    obj.flush();
    org.junit.Assert.assertNotNull("Expected 'MyClass' identifier to no longer be null", obj.getCode());
    }


    Can anybody help me with that?

    We are at the first stage, when we need to decide whether or not use Roo for the development.

    Thank you very much.
    Last edited by jbbarquero; Apr 28th, 2010, 07:37 AM.

  • #2
    I've found the solution to the problem after reading this POST: http://forum.springsource.org/showthread.php?t=84078.

    The problem resides in the JPA annotation:

    @GeneratedValue(strategy = GenerationType.AUTO)

    I need to dispense with the generated IDs due to the definition of the table in the database.

    Thanks for reading.

    Comment

    Working...
    X