【问题标题】:Hibernate with notation. Add autoincrement in primary key column in postgreSQL使用符号休眠。在 postgreSQL 的主键列中添加自动增量
【发布时间】:2017-10-03 10:59:00
【问题描述】:

我有一张桌子: 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' 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。

【问题讨论】:

    标签: java postgresql hibernate


    【解决方案1】:

    像这样修改,

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

    【讨论】:

    • 错误在 session.save(entity):org.hibernate.id.IdentifierGenerationException: ids 必须在查询调用 save() 之前手动分配。
    • 这里你使用的是序列而不是自动生成。你可以删除并尝试只使用自动生成。第二件事如果我们使用模型类,通常实现可序列化的接口。因为对象映射是通过序列化(ORM工作)完成的。可能也是一个原因。我们要考虑的第三件事是正确实现 POJO 模型类。意味着,使用适当的构造函数来分类。我已经使用相同的方法添加了我的课程。所以你可以修改你的序列并尝试使用这种简单的方式。它会工作。不需要复杂的实现。
    • 我尝试了不同的符号。似乎hibernate根本没有被识别或没有提到注释。如果 SEQUENCE、AUTO 或没有符号,即使没有 @Id,也会出现相同的行为 - 只需始终尝试插入 id 为 0 的行。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 2011-03-25
      • 2014-02-10
      • 2010-10-11
      • 2016-09-01
      • 2011-01-01
      相关资源
      最近更新 更多