[JIRA] (HHH-15767) missing unique constraints from optional @OneToOne
by Gavin King (JIRA)
Gavin King ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNDc4YWJlMDg3... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-15767?atlOrigin=eyJpIjoiNDc4YW... ) HHH-15767 ( https://hibernate.atlassian.net/browse/HHH-15767?atlOrigin=eyJpIjoiNDc4YW... ) missing unique constraints from optional @OneToOne ( https://hibernate.atlassian.net/browse/HHH-15767?atlOrigin=eyJpIjoiNDc4YW... )
Issue Type: Bug Assignee: Unassigned Components: hibernate-core Created: 26/Nov/2022 07:00 AM Priority: Major Reporter: Gavin King ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Currently Hibernate never automatically produces unique constraints on foreign key columns mapped using @OneToOne unless the association is explicitly marked optional=false. There are two a comments in the source code to the effect that this is intentional because “some databases like Derby” don’t support that. Well, as of 2022, Derby does seem to support that, and even if it didn’t, having the limitations of Derby of all things constrain what we do on real databases seems just … completely wrong.
(Damn, feels like we need some sort of thing that abstracts over the differences between SQL dialects. Really wish we had a thing like that…)
Now, the problem is that, without the unique constraint, there is simply no difference at all between @OneToOne and @ManyToOne and so you can use @OneToOne as a bad @ManyToOne and, well, apparently people do, because fixing this broke multiple badly-written tests!
So look, there’s simply no excuse for not fixing this. Yes, I understand that in principle some people’s programs will break. (This will really only affect tests, since that is, we hope, where schema export is mostly used.) The current situation is untenable.
( https://hibernate.atlassian.net/browse/HHH-15767#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15767#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#100210- sha1:eab5823 )
2 years, 10 months
[JIRA] (HHH-15766) UNION query doesn't preserve the original semicolons, and so it fails on PostgreSQL
by Vlad Mihalcea (JIRA)
Vlad Mihalcea ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZTQ0NDkwMmM2... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-15766?atlOrigin=eyJpIjoiZTQ0ND... ) HHH-15766 ( https://hibernate.atlassian.net/browse/HHH-15766?atlOrigin=eyJpIjoiZTQ0ND... ) UNION query doesn't preserve the original semicolons, and so it fails on PostgreSQL ( https://hibernate.atlassian.net/browse/HHH-15766?atlOrigin=eyJpIjoiZTQ0ND... )
Issue Type: Bug Assignee: Unassigned Components: hibernate-core Created: 26/Nov/2022 06:56 AM Priority: Major Reporter: Vlad Mihalcea ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
If we execute a query like this one on PostgreSQL:
List<String> topics = entityManager.createQuery("""
(
select c.name as name
from Category c
order by c.id
)
union
(
select t.name as name
from Tag t
order by t.id
)
""", String.class)
.getResultList();
The following exception is thrown:
n.t.d.l.l.SLF4JQueryLoggingListener - Name:DATA_SOURCE_PROXY, Connection:6, Time:20, Success:False, Type:Prepared, Batch:False, QuerySize:1, BatchSize:0, Query:["select c1_0.name from category c1_0 order by c1_0.id union select t1_0.name from tag t1_0 order by t1_0.id"], Params:[()]
o.h.e.j.s.SqlExceptionHelper - SQL Error: 0, SQLState: 42601
o.h.e.j.s.SqlExceptionHelper - ERROR: syntax error at or near "union"
That’s because the generated SQL query does not preserve the semicolons:
select c1_0.name from category c1_0 order by c1_0.id
union
select t1_0.name from tag t1_0 order by t1_0.id
and this one fails in PostgreSQL.
If the semicolons were preserved, then this SQL query would have worked just fine:
(select c1_0.name from category c1_0 order by c1_0.id)
union
(select t1_0.name from tag t1_0 order by t1_0.id)
( https://hibernate.atlassian.net/browse/HHH-15766#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15766#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#100210- sha1:eab5823 )
2 years, 10 months