Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPA/Hibernate don't manage to insert an entity with H2 2.0.202 (was fine with 1.4.200) #3265

Closed
zapek opened this issue Dec 15, 2021 · 3 comments

Comments

@zapek
Copy link

zapek commented Dec 15, 2021

I have the following table:

CREATE TABLE locations
(
	id                  IDENTITY    NOT NULL PRIMARY KEY,
	profile_id          BIGINT      NOT NULL,
	...

and the following entity:

@Table(name = "locations")
@Entity
public class Location
{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
        ...

When running the unit tests, consisting of a simple locationRepository.save(location1);, I get the following error:

could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:
insert into locations (id, connected, dht, discoverable, last_connected, location_identifier, name, net_mode, profile_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?) [23502-202]

I tried removing the "NOT NULL" statement when creating the table for the "id" column but it doesn't change anything.

This is hibernate 5.6.1. I didn't report it to them yet because it used to work fine with H2 1.4.200 so I'm not sure where the problem really is.

@katzyn
Copy link
Contributor

katzyn commented Dec 16, 2021

It's a bug of Hibernate.

H2IdentityColumnSupport.getIdentityInsertString() incorrectly returns "null", but it should return "DEFAULT" for all versions of H2, just like for any other normal database system and there are other problems with this class and H2Dialect too, H2 2.0 is very different from 1.4 and needs some adjustments, I think they performed some of them before release of H2 2.0.202, but this work wasn't completed.

https://github.com/hibernate/hibernate-orm/blob/28b8b33b88bc5516b18cf0c9343276d2cb934047/hibernate-core/src/main/java/org/hibernate/dialect/identity/H2IdentityColumnSupport.java#L31

You can create a feature request about support of H2 2.0 here:
https://hibernate.atlassian.net/jira/software/c/projects/HHH/issues

You can add DEFAULT ON NULL to definition of you column as a workaround, but you may run into some other issue immediately.

ID BIGINT GENERATED BY DEFAULT AS IDENTITY DEFAULT ON NULL PRIMARY KEY

@digulla
Copy link

digulla commented Dec 16, 2021

For all people who struggle with the H2Dialect: It's relatively easy to extend the base class and put your custom setup in it and then use your customized dialect with Hibernate. So you don't have to wait for the next Hibernate release to get some workaround.

@katzyn
Copy link
Contributor

katzyn commented Dec 19, 2021

Issue about wrong implementation of H2IdentityColumnSupport.getIdentityInsertString() is here:
https://hibernate.atlassian.net/browse/HHH-14985

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants