我需要为表格中的字段设置自动增量。
这是我的实体类:
@Entity
@Table(name = "UserLogin")
public class User implements Serializable {
private int id;
private String username;
private String password;
private String fname;
private String lname;
private String emailId;
private boolean loginperson;
private String manager;
@Id
@GeneratedValue
@Column(name = "empolyeeId")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "username", nullable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password", nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "firstName", nullable = false)
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
@Column(name = "lastName", nullable = false)
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
@Column(name = "emailId", nullable = false)
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public void setManager(String manager) {
this.manager = manager;
}
@Column(name = "loginperson", nullable = false)
public boolean isLoginperson() {
return loginperson;
}
public void setLoginperson(boolean loginperson) {
this.loginperson = loginperson;
}
@Column(name = "manager", nullable = false)
public String getManager() {
return manager;
}
public String getEmailId() {
return emailId;
}
这里我无法为id字段设置Generated value。但它没有为id字段创建自动增量。
我试过
@GeneratedValue(strategy=GenerationType.AUTO)
和
@GeneratedValue(strategy=GenerationType.IDENTITY)
但是这个表没有在mysql数据库中创建。
我收到以下日志。
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table UserLogin (empolyeeId integer generated by default as identity (start with 1), emailId varchar(255), firstName varchar(255) not null, lastName varchar(255) not null, loginperson bit not null, manager varchar(255) not null, password varchar(255) not null, username varchar(255) not null, primary key (empolyeeId))
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generated by default as identity (start with 1), emailId varchar(255), firstName' at line 1
答案 0 :(得分:4)
尝试将这些行用于您的ID:
@Id
@GenericGenerator(name="gen",strategy="increment")
@GeneratedValue(generator="gen")
@Column(name = "ID", unique = true, nullable = false, precision = 15, scale = 0)
private Long id;
最好使用Long,但如果坚持使用int,只需将类型更改为int。
答案 1 :(得分:2)
检查persistence.xml文件以查找数据库方言的内容并将其更改为:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
然后试试。我有类似的问题,这解决了它。
答案 2 :(得分:1)
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "empolyeeId")
private int id;
答案 3 :(得分:1)
最好的方法是首先在数据库中创建一个序列并将其分配给您的实体,如下所示 -
@Entity
@Table(name = "UserLogin")
@SequenceGenerator(name="loginSeq", sequenceName="SEQ_LOGIN", allocationSize=1)
public class User implements Serializable {
然后使用此序列自动递增,如下所示 -
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "loginSeq")
@Column(name = "empolyeeId")
public int getId() {
return id;
}