Skip to content

HHH-14985 H2Dialect does not work properly with h2 2.0.202 on inserts #4524

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

Merged
merged 1 commit into from
Jan 5, 2022

Conversation

froque
Copy link
Contributor

@froque froque commented Dec 27, 2021

This should be cherry picked into Hibernate 5.x

@gavinking
Copy link
Member

From which version of h2 is this change necessary?

@froque
Copy link
Contributor Author

froque commented Dec 27, 2021

works with H2 1.3.176

@gavinking
Copy link
Member

What i'm saying is that it should be written so that it checks the version number and adapts to the version of h2.

@vxel
Copy link

vxel commented Dec 28, 2021

The change is necessary for all H2 2.0.x versions, i.e. starting at H2 2.0.202
See :
h2database/h2database#3231

@gavinking
Copy link
Member

gavinking commented Dec 28, 2021

OK, great, then @froque would you please make the change to this PR to reflect that?

Do you understand what I'm asking?

@katzyn
Copy link

katzyn commented Dec 28, 2021

DEFAULT should be used for all versions of H2 unconditionally, it works with all versions. Even H2 1.0 (2006-12-17) supports it well.

Attempt to insert NULL into column with NOT NULL constraint is wrong by itself. H2 1.X accepted it for identity columns only, but H2 2.X follows the Standard here in default configuration and doesn't allow it any more.

@gavinking
Copy link
Member

DEFAULT should be used for all versions of H2 unconditionally

Ah, OK, then that's different.

@katzyn
Copy link

katzyn commented Dec 28, 2021

Just for information: getIdentitySelectString() isn't compatible with H2 2.0 too and there is nothing to return for 2.0.202, 2.0.204 and later versions. 1.4.200 was the last version where current implementation can be used (with caution, because IDENTITY() isn't a reliable function, for example, it may return value from another table if trigger performed some operations.)

In Regular mode H2 2.0 provides only two normal ways to query what was actually inserted:

  1. Standard data change delta tables: SELECT ID FROM FINAL TABLE (INSERT INTO TEST(C1, C2) VALUES (1, 2))
  2. Statement.getGeneratedKeys(), if JDBC is used.

Few compatibility modes provide additional legacy functions for identity columns.

It looks like GetGeneratedKeysDelegate can be used for 2.0.202 and newer versions and perhaps it will be better to use it since H2 1.4.197, but it needs to be tested.

In 1.4.196 and older versions getGeneratedKeys() was implemented with SELECT SCOPE_IDENTITY() by itself, it means result set doesn't know real name of the column. But SCOPE_IDENTITY() at least doesn't return unexpected values from triggers.

Copy link
Member

@beikov beikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for information: getIdentitySelectString() isn't compatible with H2 2.0

If java.sql.DatabaseMetaData#supportsGetGeneratedKeys returns true, which it does, it will use JDBC getGeneratedKeys, so this should be fine.

Changes look good. IMO we can merge this.

@gavinking
Copy link
Member

@beikov but ... perhaps we should do this instead:

#4569

I'm not sure.

@beikov
Copy link
Member

beikov commented Jan 3, 2022

I think supportsInsertSelectIdentity should be conditional based on the dialect version as I think the final table syntax is only supported as of 1.4.200. Is that correct @katzyn ?

@gavinking
Copy link
Member

gavinking commented Jan 3, 2022

I think supportsInsertSelectIdentity should be conditional based on the dialect version as I think the final table syntax is only supported as of 1.4.200. Is that correct @katzyn ?

Right, could be. But it's also unclear if it's actually better than using getGeneratedKeys() or whether they're the same, so I'm really not clear on anything here.

@gavinking
Copy link
Member

gavinking commented Jan 3, 2022

Also, I mean, the Hibernate test suite doesn't run at all on H2 2. Many test failures. No idea if lots of things are broken or just one big thing.

@katzyn
Copy link

katzyn commented Jan 4, 2022

as I think the final table syntax is only supported as of 1.4.200

Data change delta tables are supported since H2 1.4.200, but they have some issues in this outdated version. They were resolved in H2 2.0.202.

Statement.getGeneratedKeys() returns values form the same stage as FINAL TABLE, but it is currently optimized a little bit better, because it collects values only from requested columns (data change delta tables collect complete rows).

@katzyn
Copy link

katzyn commented Jan 4, 2022

No idea if lots of things are broken or just one big thing.

Possibly many. INFORMATION_SCHEMA in H2 2.0 is compliant with the SQL Standard as it should in every DBMS, but it is very different from what H2 1.x has. Some non-standard additional tables were also modified to represent complete correct information.

It looks like Hibernate can't find indexes any more, it needs to use different queries for H2 2.0 and 1.*.

@beikov beikov merged commit f64f311 into hibernate:main Jan 5, 2022
@beikov
Copy link
Member

beikov commented Jan 5, 2022

Thanks!

yvoswillens added a commit to flowable/flowable-engine that referenced this pull request Jan 10, 2022
can be changed backed when hibernate/hibernate-orm#4524 is on Hibernate 5.6.x
yvoswillens added a commit to flowable/flowable-engine that referenced this pull request Feb 10, 2022
can be changed backed when hibernate/hibernate-orm#4524 is on Hibernate 5.6.x
tijsrademakers pushed a commit to flowable/flowable-engine that referenced this pull request Feb 11, 2022
* updated h2 to 2.0.206; fixes CVE-2021-42392 vulnerability

* explicit column mapping in order to support updated h2

* added h2 column type

* delegate create, drop and update to CommonSchemaManager

* explicit column mapping in order to support updated h2

* added h2 column type

* explicit column mapping in order to support updated h2

* changed GenerationType to AUTO

can be changed backed when hibernate/hibernate-orm#4524 is on Hibernate 5.6.x
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

Successfully merging this pull request may close these issues.

5 participants