【问题标题】:Postgres Hibernate Primary Key generation IssuePostgres Hibernate 主键生成问题
【发布时间】:2017-10-31 14:38:35
【问题描述】:

我正在尝试使用 Hibernate ORM 在 Employee 表中插入一条记录,但不断收到错误消息:

org.hibernate.AssertionFailure: null id in 
com.example.helloworld.entities.Employee entry (don't flush the Session 
after an exception occurs)

如果我传递 ID 值,那么它可以正常工作,但是虽然我使用了 @GeneratedValue(strategy = GenerationType.IDENTITY),但它不会自动生成 'id'。任何帮助表示赞赏! DB 模式和实体代码的详细信息如下所示。

数据库引擎:Postgres

ORM:休眠

我有以下两个表:

表名员工


Column Name |  Type      |  Length    |  Not Null  | Default
name        | Varchar    |  64        |  true      | NULL 
id          |  int8      |  19        |  true      |nextval(Employee_id::regclass)
company_id  |  int8     |19           |  true      | NULL

表#Company

Column Name |  Type      |  Length    |  Not Null  | Default
Cname       | Varchar    |  64        |  true      | NULL 
id          |  int8      |  19        |  true      | nextval(Company_id::regclass) 

 区域 | varchar |64 |真实 |空

上述表格的实体 Java 类:

@Entity
@Table(name = "Employee")
public class Employee {

    public Employee() {super();}

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private long id;

    @NotNull
    @Column(name = "company_id")
    private long company_id;

    @NotNull
    @Column(name = "name")
    private String name;

    @ManyToOne
    private Company company;

   /**
   setters and getters
   **/
}

@Entity
@Table(name = "Company")
public class Company {

    public Company() {super();}

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private long id;

    @NotNull
    @Column(name = "area")
    private String area;

    @NotNull
    @Column(name = "cname")
    private String cname;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="company_id")
    private Set<Employee> employees;

   /**
   setters and getters
   **/
}

【问题讨论】:

    标签: java sql postgresql hibernate hibernate-mapping


    【解决方案1】:

    我对 Postgres 也有同样的问题。 尝试将SequenceGenerator 添加到您的id 字段:

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_EMPLOYEE")
    @SequenceGenerator(name="SEQ_EMPLOYEE", sequenceName="SEQ_EMPLOYEE", allocationSize=1)
    @NotNull
    private long id;
    

    SEQ_EMPLOYEE替换为数据库中序列的名称。

    【讨论】:

    • 错误没有区别。得到相同的错误,例如:
       ERROR [2017-10-31 14:47:14,721] org.hibernate.AssertionFailure: HHH000099: an assertion failure occurred (这可能表明 Hibernate 中存在错误,但更可能是由于不安全使用会话):org.hibernate.AssertionFailure:com.example.helloworld.entities.Employee条目中的空id(发生异常后不要刷新会话)
    • 我也忘了更改 GeneratedValue。我已经编辑了我的帖子。
    • 谢谢,@Wesley 成功了!它从 10 开始生成 ID,我不明白为什么我也有几个问题如下: 1) SEQ 类型生成不会在唯一性方面产生任何问题吗? 2) 可以生成的 id 数量有任何限制吗? 3)为什么“身份生成”在我的情况下不起作用?它在没有外部引用的单个表中工作。
    【解决方案2】:

    这对我有用:

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator="seq_name_generated_in_db")
    @SequenceGenerator(name="seq_name_generated_in_db", sequenceName="seq_name_generated_in_db", initialValue = 1 , allocationSize=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多