英文:
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
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:
英文:
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" />
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论