[JIRA] (HHH-16019) @Where not applied across association boundaries
by Jaehoon Lee (JIRA)
Jaehoon Lee ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=61d510f... ) *commented* on HHH-16019 ( https://hibernate.atlassian.net/browse/HHH-16019?atlOrigin=eyJpIjoiODQxMm... )
Re: @Where not applied across association boundaries ( https://hibernate.atlassian.net/browse/HHH-16019?atlOrigin=eyJpIjoiODQxMm... )
@Steve Ebersole
Thank you for your response and I apologize for the late reply. I've been on vacation and haven't checked in.
>
>
>
> "I assume you run the tests against a pre-existing schema"
>
>
I ran the tests using the H2 memory database set up in hibernate-test-case-templates.
>
>
>
> "If Hibernate exports the schema, it will (properly) create a unique
> constraint for the @OneToOne used on UserDetail#user"
>
>
The is_active column on the UserDetail entity is a flag used for soft-delete purposes, so there can be multiple rows with the same user_id in the database.
In versions 5.6.14.Final , 5.6.15.Final , 6.1.6.Final , and 6.1.7.Final , schema generation does not seem to create a unique constraint on that field.
The following is the DDL for the user_details table generated by version 6.1.7.Final :
create table user_details (
detail_id bigint generated by default as identity ,
is_active boolean ,
city varchar (255),
user_id bigint ,
primary key (detail_id)
)
However, I found that the 6.2.0.CR1 and 6.2.0.CR2 versions create their own constraints on those fields during schema generation.
The following is the DDL for the user_details table generated by the 6.2.0.CR2 version:
create table user_details (
is_active boolean ,
detail_id bigint generated by default as identity ,
user_id bigint unique ,
city varchar (255),
primary key (detail_id)
)
As you can see, the unique constraint has been added to the user_id column.
Perhaps this is an intentional change in behavior in version 6.2.0?
Anyway, in 6.2.x and later versions, the unique constraint created as above causes a "Unique index or primary key violation" error when inserting sample data for testing, but that doesn't seem to be very relevant to this issue, so let's get back to the original issue.
>
>
>
> "There is a lot going on in your tests, is that really the minimally
> reproducible domain model?"
>
>
I apologize that my tests are verbose and unclear. I have simplified the test code as much as I could.
Please check it out the following repository (the HHH-16019-2 branch):
* https://github.com/dev-jaehoonlee/hibernate-test-case-templates/tree/HHH-... ( https://github.com/dev-jaehoonlee/hibernate-test-case-templates/tree/HHH-... )
* Hibernate 5 test case: /orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java ( https://github.com/dev-jaehoonlee/hibernate-test-case-templates/blob/HHH-... )
* Hibernate 6 test case: /orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java ( https://github.com/dev-jaehoonlee/hibernate-test-case-templates/blob/HHH-... )
In versions 5.6.14.Final , 5.6.15.Final , the following query is generated on find() , and the test passes.
select
user0_.user_id as user_id1_1_0_,
user0_.user_name as user_nam2_1_0_,
userdetail1_.detail_id as detail_i1_0_1_,
userdetail1_.is_active as is_active2_0_1_,
userdetail1_.city as city3_0_1_,
userdetail1_.user_id as user_id4_0_1_
from
users user0_
left outer join
user_details userdetail1_
on user0_.user_id=userdetail1_.user_id
and (
userdetail1_.is_active = true
)
where
user0_.user_id=?
On the other hand, in versions 6.1.6.Final , 6.1.7.Final , the following query is generated on find().
select
u1_0.user_id,
d1_0.detail_id,
d1_0.is_active,
d1_0.city,
d1_0.user_id,
u1_0.user_name
from
users u1_0
left join
user_details d1_0
on u1_0.user_id=d1_0.user_id
where
u1_0.user_id=?
Despite the @Where(clause = "is_active = true") condition present in the UserDetail entity, the and ( userdetail1_.is_active = true ) statement is omitted from the on clause of the join.
Therefore, two rows are returned, which results in a "Duplicate row was found" error and the test fails.
What I expect is for the condition d1_0.is_active = true to exist in the on clause of the join, as it did in versions 5.6.14.Final , 5.6.15.Final.
Again, I apologize for the late response and thank you.
( https://hibernate.atlassian.net/browse/HHH-16019#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16019#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100217- sha1:077879e )
1 year, 10 months