Ville Myrskyneva
Ville Myrskyneva

Reputation: 1640

Hibernate JPA Column "NEXTVAL" not found

We changed the JPA provider from EclipseLink to Hibernate 4.1.3. A problem occured when attempting to persist entity using generated id. This gives the following error:

    JdbcSQLException: Column "NEXTVAL" not found; SQL statement: select nextval for SCHEMA.ID_SEQ_GENERATOR

The sequence for the entity looks like the following:

    @Id
    @SequenceGenerator(name = "ID_GENERATOR", initialValue = 100000, allocationSize = 1, sequenceName = "ID_SEQ_GENERATOR")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR")
    @Column(name = "ID", nullable = false)
    public Long getId() {
      return this.id;
    }

I tried changing the 'strategy = GenerationType.IDENTITY', but it had no effect.

Any advice?

Upvotes: 4

Views: 2764

Answers (1)

Ville Myrskyneva
Ville Myrskyneva

Reputation: 1640

I found out that there are different databases used depending on the environment used. The dialect used is (currently) always the same even though it should change depending on the server the code is executed on. (The correct dialect is actually loaded first, but it then gets replaced because of spring decides to use another class annotated with @Configuration. I think that using profiles should get me past this, though still working with this...)

I found this link which helped me to get on right tracks: http://techmagik.blogspot.fi/2012/07/sequence-support-in-jpa-for-db2-zos.html.html

Discovering that the H2 was used, I changed the SQL to :

"NEXT VALUE FOR " + sequenceName
  • Which fixed the error

So the actual problem is with loading correct dialect (as 'Luca Basso Ricci' hinted). The dialect is for DB2 (always), when it should be for H2 in some cases. For some reason this problem manifested when the provider was changed so it made it a bit difficult to track right reason.

Upvotes: 4

Related Questions