JpaRepository.save() 生成的 HQL 在向表中插入 null 值。

huangapple go评论111阅读模式
英文:

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&lt;User, Integer&gt; {

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

@Entity
@Table(name = &quot;user&quot;)
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=&quot;id&quot;,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=&quot;ori_id&quot;,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 = &quot;Minimum user name length: 3 characters&quot;)
	@Column(name=&quot;user_name&quot;,unique = true, nullable = false)
	private String userName;

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

List&lt;User&gt; list=userRepository.findAll();  
userRepository.deleteAll();
User admin = new User();
admin.setUserName(&quot;admin&quot;);
admin.setPassword(&quot;admin&quot;);
admin.setEmail(&quot;admin@email.com&quot;);
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 &#39;create_time&#39; doesn&#39;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.

huangapple
  • 本文由 发表于 2020年9月30日 10:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64130074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定