Taher Galal
Taher Galal

Reputation: 327

hibernate could not get next sequence value

I have a class that is a table within a postgress database. The class is then extended by the other classes. The main class has the following annotation at the top

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

@Entity
@Table(name="policy_action")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class PolicyAction {


  @Id
  @GeneratedValue(strategy=GenerationType.TABLE)
  @Column(name="id")
  private int id;
  .....

the second class

public class myspecialPolicy extends policy {
    ....
}

when inserting into the database the following error is thrown

org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist

I am not sure how to solve this problem any help will be grateful thanks in advance.

Upvotes: 0

Views: 1969

Answers (4)

abshar
abshar

Reputation: 475

One reason to this issue can be the PK is not connected to the sequence. For example if you perform migration from Postgres 9 to 12, tables and sequences can be generated, but pks in tables not geting value from seq.nexVal. This cause null value in column "id" violates not-null constraint What I did to fix this was running this scrip to make the conection:

alter table schema_name.TableName alter column id set default nextval(‘schema_name.seq_name’::regclass);

Upvotes: 0

Johnyoat
Johnyoat

Reputation: 456

What solved my problem was creating a hibernate_sequence table with nxt_val column with a long datatype in my database.

Sql Create hibernate_sequence(nxt_val long);

and then

Insert into hibernate_sequence (nxt_val) values ('12') NB the value 12 can be any number.

Now what happens is, hibernate will pick the value in the nxt_val to determine the sequence generation with specifying anything. It works for well for TableGenerator, Sequence, Auto

Upvotes: 0

If you use a serial value you should use :

@Entity
@Table(name="policy_action")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class PolicyAction {


  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="genName")
  @SequenceGenerator(name="genName", sequenceName="yourSequenceNameInDatabase",initialValue=1,allocationSize=1)
  @Column(name="id")
  private int id;

If you want to use the Table Strategy, you have to create a Table with two columns (seqName and value, and sequenceTable for the table name, for example) and do this :

@Entity
@Table(name="policy_action")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class PolicyAction {


  @Id
  @GeneratedValue(strategy=GenerationType.Table,generator="genName")
  @TableGenerator(name="genName", table="sequenceTable", pkColumnName="seqName",valueColumnName="value",pkColumnValue="policyAction",initialValue=1,allocationSize=1)
  @Column(name="id")
  private int id;

Upvotes: 2

Cristian Meneses
Cristian Meneses

Reputation: 4041

Depending of your version of Hibernate, one of these mappings can help you

@Column(name = "id", columnDefinition = "serial")
@Generated(GenerationTime.INSERT)
private int id;

On recent Hibernate versions (4.3 and later), you can use this:

@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

Upvotes: 0

Related Questions