0

In my Spring boot+ hibernates+mysql application I have my own UserRepository :

public interface UserRepository extends JpaRepository<User, Integer> { 

And I have a user table in mysql database, and a corresponding entity class.

@Entity @Table(name = "user") public class User { /** * This field was generated by MyBatis Generator. This field corresponds to the database column user.id * @mbg.generated Mon Sep 28 14:41:58 CST 2020 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id",length=32) // private Integer id; /** * This field was generated by MyBatis Generator. This field corresponds to the database column user.ori_id * @mbg.generated Mon Sep 28 14:41:58 CST 2020 */ @Column(name="ori_id",length=32) // private Integer oriId; /** * This field was generated by MyBatis Generator. This field corresponds to the database column user.user_name * @mbg.generated Mon Sep 28 14:41:58 CST 2020 */ @Size(min = 3, max = 18, message = "Minimum user name length: 3 characters") @Column(name="user_name",unique = true, nullable = false) private String userName; 

the findAll method works fine, but when I created a User object and call save method:

List<User> list=userRepository.findAll(); userRepository.deleteAll(); User admin = new User(); admin.setUserName("admin"); admin.setPassword("admin"); admin.setEmail("[email protected]"); admin.setCreateTime(new Date()); admin.setRole(Role.ROLE_ADMIN.name()); userRepository.save(admin); 

the hql generated is:

Hibernate: insert into user (create_ime, email, last_user_ime, last_password_change, mobile, ori_id, password, real_name, role, role_id, status, user_count, user_name, user_ime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 

which seems that the save method is saving a object with all fields null and I got the error:

2020-09-30 10:24:24.657 WARN 15956 --- [ restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1364, SQLState: HY000 2020-09-30 10:24:24.662 ERROR 15956 --- [ restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : Field 'create_time' doesn't have a default value 

How can I fix this?

1
  • Only the fields that you do not provide a value for will be null. The DB says that a value should be provided. Commented Sep 30, 2020 at 2:45

1 Answer 1

2

The (?, ?, ?, ...) is normal in hibernate logs, the actual parameter values are not logged, that doesn't mean that the inserted values are null.

Note that the query is inserting the field "create_ime" and the error says the column that does not accept nulls is "create_time", so it seems you have both columns in the table, probably caused by a typo correction or rename.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.