带有符号的冬眠.在PostgreSQL中的主密钥列中添加自动插入



我有一个表:表

和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" 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.istifierGenerationEx‌ception:该类的ID必须在Qualer called call save save()。

之前手动分配

这样修改,

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

我非常专心。我忘了在生成的配置文件Hibernate.cfg.xml中删除字符串。但是出现另一个问题之后 - 错误:关系的列" firstName" users_tb"不存在。我的代码根本不包含" firstName"!好的 - Alter Table User_tb将列firs_name重命名为firstName,以及其他列。所有使用策略= generatype.auto。

最新更新