New entity persisted with NULL id (id=0)

37 Views Asked by At

I'm using Hibernate 4.3.11. I have the following entity:

@Entity
@Table(name="ESP_RETARD")
public class ESP_RETARD implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "pk_seqret", sequenceName = "ESP_RETS_SEQ1", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_seqret")
    @Column(nullable = false)
    @NotNull
    private long idRetard;

I tried to persist a new entity on ESP_RETARD.

But, I encounter a problem: a new entity is added, however, it's with id 0.

the second time to add a new one, I got this exception:

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

...

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement

...

Caused by: java.sql.SQLException: ORA-00001: unique constraint (EDTINGA.SYS_C009525) violated

Knowing that: when I used:

  • Hibernate 3 with those jars, I didn't get this issue.
  • But once I upgraded to Hibernate 4.3.11 with those jars, I encountred this excpetion.

==> So it's a problem of jars, I suppose that it's missed jar(s) have to be added to Hibernate 4.3.11.

Have you please any idea about solving that issue ?. Big thanks Sir.

1

There are 1 best solutions below

0
Saria Essid On BEST ANSWER

You can try removing the entire entity and then re-create it with new table name and new column name for the id.

Don't forgot to change the nomination on @SequenceGenerator and @GeneratedValue.

Indeed, it's not necessary to use neither @Column(nullable = false) nor @NotNull.

    @Id
    @SequenceGenerator(name = "pk_seqret1", sequenceName = "RETS_SEQ1", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk_seqret1")
    private long idRetard;

HTH.