2

I've writing a rest service using spring boot and spring data jpa. I got problem when inserting a data to database. I got searched but not figure out the problem.

So, I have a table in Mysql called user.

This is the DDL:

'id' INT NOT NULL AUTO_INCREMENT,
'registerId' INT NULL,
'genderId' INT NULL,
'cityId' INT NULL,
'firstName' VARCHAR(255) NULL,
'lastName' VARCHAR(255) NULL,
'username' VARCHAR(255) NULL,
'age' INT(2) NULL,
'email' VARCHAR(100) NULL,
'isNotificationAllowed' BIT(1) NULL,
'isBlocked' BIT(1) NULL,
'isActive' BIT(1) NULL,
'createdAt' TIMESTAMP NULL,
'updatedAt' TIMESTAMP NULL,

And this is the Entity represent those columns in java project.

@Data
@Builder
@Entity
@Table(name = "user")
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private int id;

    @Column(name = "registerId", nullable = true)
    private int registerId;

    @Column(name = "genderId", nullable = true)
    private Integer genderId;

    @Column(name = "cityId", nullable = true)
    private Integer cityId;

    @Column(name = "firstName", nullable = true, length = 255)
    private String firstName;

    @Column(name = "lastName", nullable = true, length = 255)
    private String lastName;

    @Column(name = "username", nullable = true, length = 255)
    private String username;

    @Column(name = "age", nullable = true)
    private Integer age;

    @Column(name = "email", nullable = true, length = 100)
    private String email;

    @Column(name = "isNotificationAllowed", nullable = true)
    private boolean isNotificationAllowed;

    @Column(name = "isBlocked", nullable = true)
    private boolean isBlocked;

    @Column(name = "isActive", nullable = true)
    private boolean isActive;

    @Column(name = "createdAt", nullable = true)
    private Timestamp createdAt;

    @Column(name = "updatedAt", nullable = true)
    private Timestamp updatedAt;
}

So, when I run the service via Postman, it shows an sql on console like this insert into user (age, city_id, created_at, email, first_name, gender_id, is_active, is_blocked, is_notification_allowed, last_name, register_id, updated_at, username, id).

As you figure out entity names are different than the column names in database. For ex: cityId in my POJO transformed city_id in sql. So, I've search it and found that there is a bug in @Column annotation. It is not working as we expected.

Please look at : Here's a link.

So,to be able to solve this problem I've added

spring.jpa.hibernate.naming.implicit- 
strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Then restarted the application and the it seems column mapping has resolved:

Hibernate: 
 insert 
 into
     user
     (age, cityId, createdAt, email, firstName, genderId, isActive, isBlocked, isNotificationAllowed, lastName, registerId, updatedAt, username, id) 
 values
     (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

But at the end of the logs, there is an error thrown. It says:

java.sql.SQLException: Field 'register_id' doesn't have a default value
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar:8.0.17]
 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar:8.0.17]
 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.17.jar:8.0.17]

I don't understand where the fieds register_id exits. It doesn't exist in database or entity.

When I don't send registerId as parameter in the request, it says java.sql.SQLIntegrityConstraintViolationException: Column 'registerId' cannot be null. Also didn't figure out that message. Because it is nullable in database and defined nullable in entity.

12
  • If the field names on the Java entity match the column name you don't need to add the name=registerId part to your @Column annotation, you can just do @Column(nullable = true). Commented Oct 30, 2019 at 19:54
  • spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl did you put these properties? Commented Oct 30, 2019 at 19:59
  • @JonathanJohx yes, I've explain it in the question. Commented Oct 30, 2019 at 20:38
  • Does it work? what errors do you have? those errors that you added this question? Commented Oct 30, 2019 at 20:40
  • @JonathanJohx I've explain it step by step in the question. Yes the error is still exist. Commented Oct 30, 2019 at 20:42

1 Answer 1

1

The problem is that the registerId property is defined as int, not as Integer. the appropriate column can be null in the database, but that null value cannot be converted to int. Change it to Integer.

Sign up to request clarification or add additional context in comments.

2 Comments

I've changed it data type but still the same error thrown.
Updated the question with new case at the end of the question explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.