0

The following code only renders an ID of 0

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="PROJECT_SEQ_GEN")
@SequenceGenerator(name="PROJECT_SEQ_GEN", sequenceName="X.X_PROJECT_SEQ", allocationSize=1)
private long projectId;

I tried SEQUENCE and AUTO but both with the same result. x_project_seq.nextval works fine in my Oracle database. I would like to keep using the sequence already defined in the database.

According to the Getting Started guide by Spring - Accessing Data with JPA ...

"The Customer’s `id property is annotated with @Id so that JPA will recognize it as the object’s ID. The id property is also annotated with @GeneratedValue to indicate that the ID should be generated automatically."

From their example it looks like all they did was create new Customer and the IDs were automatically generated. What am I missing here?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
otgw
  • 390
  • 1
  • 5
  • 16

2 Answers2

2

You are using the wrong GenerationType for your implementation. Update your strategy declaration to use strategy = GenerationType.SEQUENCE and simply use PROJECT_SEQ_GEN for the sequenceName value.

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PROJECT_SEQ_GEN")
@SequenceGenerator(name="PROJECT_SEQ_GEN", sequenceName="PROJECT_SEQ_GEN", allocationSize=1)
private long projectId;
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
  • I wrote in my post that I have tried both (based on some answers I've read in other posts) – otgw Mar 31 '15 at 14:40
0

Took me all morning but I finally fixed the issue.

I was using Hibernate tools to auto generate POJOs and all the annotations were being placed at the method level, however, Spring recommends (requires?) them at the field level. You can't just move the Id annotations to the field level either, because it's either one or the other. So I followed this answer to customize Hibernate tools to generate POJOs with the annotations all at the field level.

Now it's all good.

Community
  • 1
  • 1
otgw
  • 390
  • 1
  • 5
  • 16