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?

1
  • Your strategy = GenerationType.AUTO should be strategy = GenerationType.SEQUENCE Commented Mar 31, 2015 at 0:17

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I wrote in my post that I have tried both (based on some answers I've read in other posts)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.