1

I'm trying to use JPA to generate IDs from sequences in my database (Oracle 9i)

From what I found here and there, here is the group of annotations I've set on my ID variable :

@Id
@SequenceGenerator(name="PROCEDURENORMALE_SEQ_GEN", sequenceName = "PROCEDURENORMALE_SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "PROCEDURENORMALE_SEQ_GEN")
@Column(name = "IDPROCEDURENORMALE", unique = true, nullable = false, precision = 10, scale = 0)
private long idProcedureNormale;

However, whenever I create a new object, this id is always set to 0, and because of that I can't persist data. I've tried to change the strategy from GenerationType.SEQUENCE to GenerationType.AUTO, nothing changed. For this specific table, Sequence number is supposed to be around 8300.

Where did I go wrong ?

Gyoo
  • 209
  • 3
  • 15
  • 1
    The ID is not generated when you create the object. It's generated when you persist it. – JB Nizet Jun 30 '16 at 16:19
  • Check this out for sequencenumber generation [Java - JPA - Generators - @SequenceGenerator](http://stackoverflow.com/questions/2595124/java-jpa-generators-sequencegenerator) – Rishal Jun 30 '16 at 16:30
  • and what does the log say? – Neil Stockton Jun 30 '16 at 16:55
  • Thanks @JBNizet for this information. The documentation I found on Oracle pages was unclear to me about this point. I actually found the solution to my problem, which is posted as an answer below. – Gyoo Jul 01 '16 at 07:46

1 Answers1

0

I actually solved my issue, that happened not to be directly related with what I exposed.

This object I was trying to persist is part of a relatively complex object, and in the parent object I didn't add a CascadeType to the JPA mapping annotation :

@OneToMany(fetch = FetchType.LAZY, mappedBy = "dossier")
private Set<Procedurenormale> proceduresNormales = new HashSet<>(0);

Changing this annotation to the following solved the issue :

@OneToMany(fetch = FetchType.LAZY, mappedBy = "dossier", cascade = CascadeType.ALL)
private Set<Procedurenormale> proceduresNormales = new HashSet<>(0);
Gyoo
  • 209
  • 3
  • 15