1

I have a Java program which uses Hibernate to interact with a MySQL DB with two tables, User and Status.

As I had only User Table, everything worked fine, but now I added the Status table and now I get the following exception when inserting testdata, but now for both of the tables though I didn't change anything in the User table:

Could not update entity of typ User
    Aug 12, 2016 11:37:01 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    WARN: SQL Error: 1364, SQLState: HY000
    Aug 12, 2016 11:37:01 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    ERROR: Field 'status_statusId' doesn't have a default value
    Aug 12, 2016 11:37:01 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
    INFO: HHH000010: On release of batch it still contained JDBC statements

Could not update entity of typ Status
Aug 12, 2016 11:37:01 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
        WARN: SQL Error: 1364, SQLState: HY000
        Aug 12, 2016 11:37:01 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
        ERROR: Field 'user_email' doesn't have a default value
        Aug 12, 2016 11:37:01 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
        INFO: HHH000010: On release of batch it still contained JDBC statements

Here are excerpts of my entity classes with the annotations and the variables:

User:

@Entity
@Table(name = "user")
public class User implements Serializable {

    @PrePersist
    void preInsert() {
        email = "test@testmail.com";
    }

    @Id
    @Column(name = "user_email")
    private String email;

Status:

@Entity
@Table(name = "status")
public class Status implements Serializable {

    @PrePersist
    void preInsert() {
        statusId = "A01A001";
    }

    @Id
    @Column(name = "status_statusId")
    private String statusId;

Looks like some kind of NOT NULL issue to me, but I used the @PrePersist annotation to ensure a default value in both entities.

I searched for some solutions bot none of them worked, like this one: mysql error 1364 Field doesn't have a default values

They suggest to change the mysql settings and remove STRICT_TRANS_TABLES in my.cnf. But I couldn't find this file so I tried the phpmyadmin solution. They say you should edit the sql mode variable there and remove the STRICT_TRANS_TABLES value, but when I try to edit this variable it has no value:

SQL Mode Variable Image

Does someone know another solution for this? Would be grateful for every helpful answer.

Community
  • 1
  • 1
boehmP
  • 91
  • 1
  • 1
  • 11
  • JPA callbacks don't work if you are using hibernate `session`. Read more in this thread http://stackoverflow.com/questions/4133287/preupdate-and-prepersist-in-hibernate-jpa-using-session – Predrag Maric Aug 12 '16 at 10:35
  • Thanks, I tried it with events but when I try to register my Listener, it tells me the class DefaultPreInsertEventListener does not exist. What should i use instead? Also when I remove the Status Class from Mappings and only insert User then everything works fine, it's only the Status class although I used the same structure/annotations etc. – boehmP Aug 13 '16 at 12:42

0 Answers0