gpt4 book ai didi

mysql - hibernate自动递增

转载 作者:行者123 更新时间:2023-11-29 03:57:34 27 4
gpt4 key购买 nike

我需要为我的表中的一个字段设置一个自动增量。

这是我的实体类:

@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 字段设置生成值。但它不会为 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

最佳答案

尝试使用这些行作为您的 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就可以了。

关于mysql - hibernate自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11390445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com