Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ignore fields when updating #43

Closed
tianyu94 opened this issue Sep 29, 2021 · 5 comments
Closed

How to ignore fields when updating #43

tianyu94 opened this issue Sep 29, 2021 · 5 comments

Comments

@tianyu94
Copy link

tianyu94 commented Sep 29, 2021

    @CreatedBy
    @JsonIgnore
    private Long createdBy;

    @CreatedDate
    @JsonIgnore
    private LocalDateTime createdDate;

Is there any way to ignore then (raw sql is contains: set created_by = null, set created_date = null) when updating.

Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'created_by' cannot be null
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1350) ~[mysql-connector-java-8.0.26.jar:8.0.26]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) ~[mysql-connector-java-8.0.26.jar:8.0.26]
@lpandzic
Copy link
Member

You mean like transient fields that are not present in the table?

@tianyu94
Copy link
Author

tianyu94 commented Sep 29, 2021

You mean like transient fields that are not present in the table?

Only when updating, My table has created_date and created_by fields.

When I call org.springframework.data.repository.CrudRepository#save (update an entity) the generated SQL has created fields, But they only exist when they are created, Is this spring-data-jdbc's auditing bug?

@lpandzic
Copy link
Member

It's possible but you have to do it manually by specifying a projection in your query that doesn't have the fields you don't need. Default projection contains all fields, example of customization is here - https://github.com/infobip/infobip-spring-data-querydsl#JDBCProjections.

@lpandzic
Copy link
Member

Hm, not sure about updates though (this is only for select), it'd have to have tighter integration with Spring Data JDBC.

@tianyu94
Copy link
Author

It may be a spring-data-jdbc problem, I can only temporarily attach the corresponding field when updating to avoid it being empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants