用符号表示Hibernate。在postgreSQL中的主键列中添加自动增量

时间:2017-10-03 10:59:01

标签: java postgresql hibernate

我有一张桌子: table

和UsersTbEntity类:

import javax.persistence.*;


@Entity
@Table(name = "users_tb", schema = "public", catalog = "sbth")
public class UsersTbEntity {
    @Id
    @SequenceGenerator( name = "jpaSequence", sequenceName = "users_tb_user_id_seq", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jpaSequence")
    @Column(name = "user_id", nullable = false)
    private int userId;
    private String firstName;
    private String lastName;
    private String gender;
    private boolean married;
    private String profile;
    private java.util.Date regDate;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Basic
    @Column(name = "first_name", nullable = false, length = 40)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Basic
    @Column(name = "last_name", nullable = false, length = 40)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Basic
    @Column(name = "gender", nullable = false, length = 40)
    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Basic
    @Column(name = "married", nullable = false)
    public boolean isMarried() {
        return married;
    }

    public void setMarried(boolean married) {
        this.married = married;
    }

    @Basic
    @Column(name = "profile", nullable = false, length = 40)
    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "reg_date", nullable = true)
    public java.util.Date getRegDate() {
        return regDate;
    }

    public void setRegDate(java.util.Date regDate) {
        this.regDate = regDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UsersTbEntity that = (UsersTbEntity) o;

        if (userId != that.userId) return false;
        if (married != that.married) return false;
        if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) return false;
        if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) return false;
        if (gender != null ? !gender.equals(that.gender) : that.gender != null) return false;
        if (profile != null ? !profile.equals(that.profile) : that.profile != null) return false;
        if (regDate != null ? !regDate.equals(that.regDate) : that.regDate != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = userId;
        result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (married ? 1 : 0);
        result = 31 * result + (profile != null ? profile.hashCode() : 0);
        result = 31 * result + (regDate != null ? regDate.hashCode() : 0);
        return result;
    }
}

我想要自动增量表字段user_id。但总是插入' 0' 0 ID。在第二次插入时,我收到一个错误:

Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "users_tb_pkey"
  Detail: Key (user_id)=(0) already exists.

我也尝试过:

strategy = GenerationType.AUTO

然后我在session.save(entity)得到错误:org.hibernate.id.IdentifierGenerationEx ception:必须在调用save()之前手动分配此类的ID。

2 个答案:

答案 0 :(得分:0)

像这样修改,

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public  Integer getUserId;
// Remaining variables

public Integer getUserId() {
    return getUserId;
}

答案 1 :(得分:0)

我非常不专心。我忘了在生成的配置文件hibernate.cfg.xml中删除字符串。但在出现另一个问题后 - 错误:关系“users_tb”的列“firstname”不存在。我的代码根本不包含“名字”!确定 - ALTER TABLE users_tb RENAME COLUMN firs_name TO firstname,以及其他列。 所有与strategy一起使用= GenerationType.AUTO。