首页 > 解决方案 > "HIBERNATE_SEQUENCE" not found

问题描述

I have two tables EmployeeGroup table and EmpplyeeDetails Table. EmployeeGroupTable has primary key groupid which should be generated from databse sequence GroupIdGenerator. EmployeeDetails have two primary keys as groupid and employeeid. Groupid should be same as of the previous table and employeeid should also have incremented value like 1,2,3. These values in both table should insert in one transaction. Can you help me with correct mapping?

I already tried with different combination of Generated value and Sequence generator but not able to save the data into table.

@Entity
   @Table(name="EMPLOYEE_GROUP")
   public class EmployeeGroup {

   @Column(name = "GROUP_ID")
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = 
      "groupid-gen")
   @Id
   @NotNull
   @SequenceGenerator(name = "groupid-gen", sequenceName = 
     "GROUIP_ID_GENERATOR" )
   private  long groupId;

   @OneToMany(mappedBy = "employeeDetail")
      private List<EmployeeDetail> employeeDetails;
      }



@Entity
   @Table(name = "EMPLOYEE_DETAIL")
   @IdClass(EmployeeID.class)
   public class EmployeeDetail {

  @ManyToOne
  @JoinColumn(name = "GROUP_ID", insertable=false , updatable=false)
  private EmployeeGroup employeeGroup;

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = 
     "groupid-gen")
  @SequenceGenerator(name = "groupid-gen", sequenceName = 
    "GROUIP_ID_GENERATOR" )
  @Column(name = "GROUP_ID")
  @Nonnull
     private Long groupId;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Nonnull
  @Column(name = "EMPLOYEE_ID")
    private Long employeeId;

}

public class EmployeeId{

private Long groupId;

private Long employeeId;

public EmployeeId(final Long groupId, final Long employeeId) {
    this.groupId = groupId;
    this.employeeId = employeeId;
}

public EmployeeId() {
}

}

Expected result is both table should have proper values like.

Table EmployeeGroup

 GroupID
   1
   2

Table EmployeeDetail

GroupId       EmployeeId
  1            1
  1            2
  1            3
  2            1
  2            2

I am getting error as ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Sequence "HIBERNATE_SEQUENCE" not found; SQL statement: call next value for hibernate_sequence [90036-197] org.springframework.orm.jpa.JpaSystemException: could not prepare statement; nested exception is org.hibernate.exception.GenericJDBCException: could not prepare statement

标签: jpaspring-data-jpaopenjpajpa-2.1

解决方案


您正在混合 Id 生成器策略:

在课堂EmployeeDetail上,您有一个 Oracle 序列,groupIdGenerationType.AUTOemployeeId

GenerationType.AUTO会导致问题。

要么你必须在那里使用 Oracle 序列,要么@GeneratedValue如果这个 id 来自其他任何地方,你必须删除。


推荐阅读