We're updating the issue view to help you get more done. 

Table nullability should not depend on JpaCompliance.isJpaCacheComplianceEnabled()

Description

Part of the fix for made nullability of join tables dependent on JpaCompliance.isJpaCacheComplianceEnabled().

An example is in the SingleTableEntityPersister constructor:

1 2 3 4 5 isNullableTable[j] = join.isOptional() || creationContext.getSessionFactory() .getSessionFactoryOptions() .getJpaCompliance() .isJpaCacheComplianceEnabled();

In the discussion for HHH-12282, there was mention that we were not able to come up with an example where table nullability would caused a problem when caching multi-table entities. The related changes should have gotten reverted as part of HHH-12282, but did not.

By default, when EntityManager is used, JpaCompliance().isJpaCacheComplianceEnabled() returns true.

A side-effect is that Hibernate will not insert a row with all null values into a secondary table when that table is mapped with org.hibernate.annotations.Table(appliesTo="...", optional=false)

The original description indicates that in 5.1, when an entity join table is mapped with org.hibernate.annotations.Table(appliesTo="...", optional=false), and there is no row in the join table for that entity, that the entity was ignored; in 5.3, it is no longer ignored.

The reason it was ignored in 5.1 is that, for single-table inheritance, Hibernate uses an INNER JOIN to join the root entity's main table with the secondary table.

In 5.3, since, by default with EntityManager, JpaCompliance().isJpaCacheComplianceEnabled() returns true, Hibernate assumes that the secondary table will be nullable, even when mapped with org.hibernate.annotations.Table(appliesTo="...", optional=false), so Hibernate uses an OUTER JOIN to join the root entity's main table with the secondary table. Because of the OUTER JOIN, Hibernate no longer ignores the entity with no row in the secondary table. The fix for this issue will restore the INNER JOIN when joining a secondary table for an entity with single-table inheritance. Secondary tables for entities with joined inheritance will continue to be joined using an OUTER JOIN, as was done in 5.1.

Although Hibernate does not support having data in the database that is inconsistent with entity mappings, the bug described in the original description will be fixed.

----------------------------------------------------------------------------------------------------------------------------

Original description:

An entity is defined with a non-optional secondary table. After migrating from an older Hibernate release to Hibernate 5.3, encountering not-null constraint violations (database layer and in bean validation) in various scenarios for properties persisted in the secondary table.

It was discovered that the secondary table was corrupt and rows were missing for one or more entities. Hibernate did not load the invalid entity rows in older Hibernate releases.

Environment

None

Status

Assignee

Gail Badner

Reporter

Stephen Fikes

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

5.1.17

Components

Affects versions

5.3.9

Priority

Major