1

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

这是我的实体类:

@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 值。但它没有为 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
4

4 回答 4

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。

于 2012-07-09T07:21:51.037 回答
2

检查 persistence.xml 文件以了解数据库方言的设置并将其更改为:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

并尝试。我有类似的问题,这解决了它。

于 2012-07-09T07:25:03.990 回答
1
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "empolyeeId")
private int id;
于 2012-07-09T07:29:29.343 回答
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;
}
于 2012-07-10T12:57:02.977 回答