Search code examples
javajpawebsphere

Why insert JPA null in Database


I have a table with a last modification date. I want to set this manually in my program. Strangely, JPA reads the object correctly and the date is present, so a column error is actually not possible. However, when I create a new object now, an error message appears that JPA cannot write NULL to the LastChange Date field.(Before persist() of the EntityManager I checked if the date is NULL and it exists) I use WAS 9.0 and the Default Eclipselink with JPA 2.1


Here in my Testfall class:

@Column(Name="LetztesAenderungsdatum",insertable=true,updateable=true)
private java.sql.Date letztesAenderungsdatum

table Schema

    CREATE TABLE Test
    (
        Testfall_Id             INT (7),
        LetztesAenderungsdatum  DATE NOT NULL,
        Name                    VARCHAR (25) NOT NULL,
        Verfahren_FK    INT CONSTRAINT Verfahren_FK REFERENCES 
        Verfahren(VERFAHREN_ID),
        CONSTRAINT testfall_Id PRIMARY KEY(Testfall_Id)
    );

before Stack trace a Sysout with date: [12.06.18 13:45:14:415 CEST] 0000009f SystemOut O Testfall letztes Aenderungsdatum: 2018-06-12


stack trace:

    [12.06.18 13:45:25:289 CEST] 0000009f eclipselink   W   [eclipselink] Ausnahme [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.WAS-v20170717-04d37b5): org.eclipse.persistence.exceptions.DatabaseException
Interne Ausnahme: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: Einfügen von NULL in ("SCHEMA"."TESTFALL"."LETZTESAENDERUNGSDATUM") nicht möglich

Fehlercode: 1400
Aufruf: INSERT INTO SCHEMA.Testfall (Testfall_Id, Beschreibung, LetztesAenderungsdatum, Name, Verfahren_FK) VALUES (?, ?, ?, ?, ?)
    bind => [5 parameters bound]
Abfrage: InsertObjectQuery(myPackage.beans.Testfall@bcd7e11c)

Solution

  • You are not setting the date before calling persist. You can also include nullable = false in your annotation mapping.

    what you probably wanna do is something like this:

    @PreUpdate
    protected void onUpdate() {
        letztesAenderungsdatum = new Date();
    }
    
    @PrePersist
    protected void onPersist() {
        letztesAenderungsdatum = new Date();
    }