[JIRA] (HHH-16503) @Formula annotation doesn't generate root entity alias on where part.
by Khurelkhuyag M. (JIRA)
Khurelkhuyag M. ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYTVlNTlhYzZj... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16503?atlOrigin=eyJpIjoiYTVlNT... ) HHH-16503 ( https://hibernate.atlassian.net/browse/HHH-16503?atlOrigin=eyJpIjoiYTVlNT... ) @Formula annotation doesn't generate root entity alias on where part. ( https://hibernate.atlassian.net/browse/HHH-16503?atlOrigin=eyJpIjoiYTVlNT... )
Issue Type: Bug Affects Versions: 6.1.7 Assignee: Unassigned Attachments: hibernate-formula-problem.tar.gz Components: hibernate-core Created: 23/Apr/2023 23:07 PM Environment: Java 17, Hibernate 3.9.0, Hibernate 6.1.7 Priority: Major Reporter: Khurelkhuyag M. ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
This is my minimized sample entity. When I try to query with “ SELECT e FROM Shift e “ HQL query. It generates below SQL query.
@Entity
@Table(name = "shift" )
public class Shift {
@EmbeddedId
private PK pk;
@Column(insertable = false , updatable = false )
private LocalDate day;
@Column(insertable = false , updatable = false )
private Integer number;
@Formula("""
(SELECT json_arrayagg(
json_object(
"day" , sh.day,
"number" , sh.number,
"value" , sh.value
)
)
FROM shiftData sh WHERE sh.day = day AND sh.number = number)
""")
private String data;
@Embeddable
public static class PK implements Serializable {
@Column(name = "day" )
private LocalDate day;
@Column(name = "number" )
private Integer number;
}
}
select
s1_0. day ,
s1_0. number ,
( SELECT
json_arrayagg( json_object( " day " , sh. day , " number " , sh. number , " value " , sh. value ) )
FROM
shiftData sh
WHERE
sh. day = day
AND sh. number = s1_0. number )
from
shift s1_0
Problematic part is:
WHERE
sh. day = day
AND sh. number = s1_0. number )
This should be:
WHERE
sh. day = s1_0. day
AND sh. number = s1_0. number )
Without the alias, this selects unintended rows.
PS: Example project attached.
( https://hibernate.atlassian.net/browse/HHH-16503#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16503#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#100223- sha1:77dfe11 )
2 years, 5 months
[JIRA] (HHH-16502) Hib5: @OneToOne mapping with unique=true throws "caused a duplicate key value in a unique or primary key constraint'
by Nara Simha (JIRA)
Nara Simha ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63f1102... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMjU2NTZlYzQy... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16502?atlOrigin=eyJpIjoiMjU2NT... ) HHH-16502 ( https://hibernate.atlassian.net/browse/HHH-16502?atlOrigin=eyJpIjoiMjU2NT... ) Hib5: @OneToOne mapping with unique=true throws "caused a duplicate key value in a unique or primary key constraint' ( https://hibernate.atlassian.net/browse/HHH-16502?atlOrigin=eyJpIjoiMjU2NT... )
Change By: Nara Simha ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63f1102... )
The following mapping works fine in Hib 3.x
{noformat}@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, optional = true)
@JoinColumn(name = PAYLOAD_FILTER_COLUMN "PAYLOAD_FILTER" , unique = true)
private PayloadFilterExpression payloadFilter;{noformat}
!dampen-20230424-054645 (7efd1ac9-095c-4083-a638-335bedcd1efd).png|width=1667,height=937!
+*_The same mapping throws the following error in Hib 5.x_*+
{noformat}This table, NMS_DAMPEN_CONFIG has a one-to-one relationship with NMS_PAYLOAD_FILTER_EXPR table.
This column, PAYLOAD_FILTER is populated based on index col value of NMS_PAYLOAD_FILTER_EXPR
The unique key needs to be relaxed in order to proceed with the inserts else we hit the following issue:
"2023-04-10 01:35:02.749 ERROR [org.hibernate.engine.jdbc.batch.internal.BatchingBatch] HHH000315:
Exception executing batch [java.sql.BatchUpdateException: The statement was aborted because it would
have caused a duplicate key value in a unique or primary key constraint or unique index identified by
'UK_HJWHE1QKYLIS421SH7BTMYPH1' defined on 'NMS_DAMPEN_CONFIG'.], SQL: insert into NMS_DAMPEN_CONFIG
(UUID, VERSION, ENABLE, HR_INTERVAL, MIN_INTERVAL, PAYLOAD_FILTER, SEC_INTERVAL, ID) values (?, ?, ?, ?, ?, ?, ?, ?){noformat}
( https://hibernate.atlassian.net/browse/HHH-16502#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16502#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#100223- sha1:77dfe11 )
2 years, 5 months
[JIRA] (HHH-16502) Hib5: @OneToOne mapping with unique=true throws "caused a duplicate key value in a unique or primary key constraint'
by Nara Simha (JIRA)
Nara Simha ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63f1102... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiOTBiNjlmMzNh... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16502?atlOrigin=eyJpIjoiOTBiNj... ) HHH-16502 ( https://hibernate.atlassian.net/browse/HHH-16502?atlOrigin=eyJpIjoiOTBiNj... ) Hib5: @OneToOne mapping with unique=true throws "caused a duplicate key value in a unique or primary key constraint' ( https://hibernate.atlassian.net/browse/HHH-16502?atlOrigin=eyJpIjoiOTBiNj... )
Issue Type: Bug Affects Versions: 5.2.18 Assignee: Unassigned Attachments: dampen-20230424-054645 (7efd1ac9-095c-4083-a638-335bedcd1efd).png, dampen-20230424-054645.png Components: hibernate-orm-modules, query-criteria Created: 23/Apr/2023 22:49 PM Environment: OS: CentOS Linux release 7.9.2009
JDK: 11, Zulu
EJB 3.1, Hibernate 5.2.x,
WildFly Core 22.08.004
Database: postgreSQL as well as HSQLDB Labels: JoinColumn foreignkey uniqueconstraint Priority: Major Reporter: Nara Simha ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63f1102... )
The following mapping works fine in Hib 3.x
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, optional = true)
@JoinColumn(name = PAYLOAD_FILTER_COLUMN, unique = true)
private PayloadFilterExpression payloadFilter;
*The same mapping throws the following error in Hib 5.x*
This table, NMS_DAMPEN_CONFIG has a one-to-one relationship with NMS_PAYLOAD_FILTER_EXPR table.
This column, PAYLOAD_FILTER is populated based on index col value of NMS_PAYLOAD_FILTER_EXPR
The unique key needs to be relaxed in order to proceed with the inserts else we hit the following issue:
"2023-04-10 01:35:02.749 ERROR [org.hibernate.engine.jdbc.batch.internal.BatchingBatch] HHH000315:
Exception executing batch [java.sql.BatchUpdateException: The statement was aborted because it would
have caused a duplicate key value in a unique or primary key constraint or unique index identified by
'UK_HJWHE1QKYLIS421SH7BTMYPH1' defined on 'NMS_DAMPEN_CONFIG'.], SQL: insert into NMS_DAMPEN_CONFIG
(UUID, VERSION, ENABLE, HR_INTERVAL, MIN_INTERVAL, PAYLOAD_FILTER, SEC_INTERVAL, ID) values (?, ?, ?, ?, ?, ?, ?, ?)
( https://hibernate.atlassian.net/browse/HHH-16502#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16502#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#100223- sha1:77dfe11 )
2 years, 5 months