MySQL的GenerationType.AUTO不生成任何内容,并返回’Field doesn’t have a default value’。

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

MySql GenerationType.AUTO does not generate anything and returns 'Field doesn't have a default value'

问题

I have the following code which should auto-generate user_id (PK) in the table USER. However, when adding data to the table I get the error: ERROR 1364 (HY000): Field 'user_id' doesn't have a default value. On the other hand, when I do manually insert bigInt there is no problem and the data is added to the table, however this is not the point and I want it to be automatically generated. Can someone help me find out what is wrong?

@Entity
@Table(name="user")
public class User implements Comparable, Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="user_id")
private Long id;
@Column(name="username", unique=true)
private String username;
@Column(name="firstname", nullable =false)
private String firstname;
@Column(name="middlename", nullable =true)
private String middlename;
@Column(name="lastname", nullable =false)
private String lastname;
@Column(name="email", nullable = false)
private String email;
@Column(name="password", nullable =false)
private String password;

my persistence:

MySQL的GenerationType.AUTO不生成任何内容,并返回’Field doesn’t have a default value’。

英文:

I have the following code which should auto-generate user_id (PK) in the table USER. However, when adding data to the table I get the error: ERROR 1364 (HY000): Field 'user_id' doesn't have a default value. On the other hand, when I do manually insert bigInt there is no problem and the data is added to the table, however this is not the point and I want it to be automatically generated. Can someone help me find out what is wrong?

@Entity
@Table(name="user")
public class User implements Comparable<User>, Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="user_id")
	private Long id;
	@Column(name="username", unique=true)
	private String username;
	@Column(name="firstname", nullable =false)
	private String firstname;
	@Column(name="middlename", nullable =true)
	private String middlename;
	@Column(name="lastname", nullable =false)
	private String lastname;
	@Column(name="email", nullable = false)
	private String email;
	@Column(name="password", nullable =false)
	private String password; 

my persistence:

 <property name="hibernate.dialect" value="com.app.central.dao.pagination.MySQLDialect"/>
    	<property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.search.default.directory_provider" value="filesystem"/>
        <property name="hibernate.search.default.indexBase" value="./lucene/indexes"/>
        <property name="jboss.as.jpa.providerModule" value="org.hibernate" />
  		<property name="wildfly.jpa.hibernate.search.module" value="org.hibernate.search.orm" />

MySQL的GenerationType.AUTO不生成任何内容,并返回’Field doesn’t have a default value’。

答案1

得分: 1

@GeneratedValue(strategy=GenerationType.AUTO)只有在使用Java代码保存实体时才会生效。
您正在直接插入值到数据库中,而您的MySQL数据库并不知道它应该为ID生成任何值。
英文:

Line

@GeneratedValue(strategy=GenerationType.AUTO)

is going to have an effect only when you save an entity using Java code.

What you are doing is inserting values directly into the database and your mysql database doesn't know that it's supposed to be generated any values for the id.

huangapple
  • 本文由 发表于 2023年6月15日 06:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477960.html
匿名

发表评论

匿名网友

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

确定