英文:
JpaRepository.save() generates hql inserting null values into the tables
问题
在我的Spring Boot + Hibernate + MySQL应用程序中,我有自己的UserRepository
:
public interface UserRepository extends JpaRepository<User, Integer> {
我在mysql
数据库中有一个名为user
的表,以及相应的实体类。
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", length = 32)
private Integer id;
@Column(name = "ori_id", length = 32)
private Integer oriId;
@Size(min = 3, max = 18, message = "最小用户名长度:3个字符")
@Column(name = "user_name", unique = true, nullable = false)
private String userName;
findAll
方法运行良好,但是当我创建一个User对象并调用save
方法时:
List<User> list = userRepository.findAll();
userRepository.deleteAll();
User admin = new User();
admin.setUserName("admin");
admin.setPassword("admin");
admin.setEmail("admin@email.com");
admin.setCreateTime(new Date());
admin.setRole(Role.ROLE_ADMIN.name());
userRepository.save(admin);
生成的HQL是:
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
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
似乎save
方法保存了一个所有字段均为null
的对象,导致我得到了错误:
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
我该如何修复这个问题?
英文:
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("admin@email.com");
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
得分: 2
在 Hibernate 日志中,(?, ?, ?, ...) 是正常的,实际参数值未被记录,这并不意味着插入的值为空。
请注意,查询正在插入字段 "create_ime",而错误指出不接受空值的列是 "create_time",因此似乎您在表中同时拥有这两列,可能是由于拼写更正或重命名造成的。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论