简体   繁体   中英

JPA - Generated Id from Sequence is always 0

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 ?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM