[JIRA] (HHH-16437) class org.hibernate.mapping.BasicValue cannot be cast to class org.hibernate.mapping.ToOne
by Marius K (JIRA)
Marius K ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZTFjYTM5NWVj... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16437?atlOrigin=eyJpIjoiZTFjYT... ) HHH-16437 ( https://hibernate.atlassian.net/browse/HHH-16437?atlOrigin=eyJpIjoiZTFjYT... ) class org.hibernate.mapping.BasicValue cannot be cast to class org.hibernate.mapping.ToOne ( https://hibernate.atlassian.net/browse/HHH-16437?atlOrigin=eyJpIjoiZTFjYT... )
Issue Type: Bug Affects Versions: 6.2.0.CR4, 6.2.0 Assignee: Unassigned Attachments: hibernate-orm-6.zip, image-20230405-073033.png Components: hibernate-core Created: 05/Apr/2023 00:34 AM Priority: Major Reporter: Marius K ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... )
Hi,
I have a One-To-Many relation with the Many-entity having an composite id with AttributeOverrides. This works fine with 6.2.0-CR3 and starts failing with CR4.
java.lang.ClassCastException: class org.hibernate.mapping.BasicValue cannot be cast to class org.hibernate.mapping.ToOne (org.hibernate.mapping.BasicValue and org.hibernate.mapping.ToOne are in unnamed module of loader 'app')
I attached a test case.
hibernate-orm-6.zip ( https://hibernate.atlassian.net/secure/attachment/50293/50293_hibernate-o... )
@Entity
public class Contract implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer contractId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "id.contractId")
@OrderBy(value = "id.positionNumber")
private List<Contractposition> contractpositions = new ArrayList<>();
@Entity
public class Contractposition implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@AttributeOverrides(
{
@AttributeOverride(name = "contractId", column = @Column(name = "VERTRAG_ID", nullable = false)),
@AttributeOverride(name = "positionNumber", column = @Column(name = "POSITIONSNUMMER", nullable = false))})
private ContractpositionId id;
@Embeddable
public class ContractpositionId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "VERTRAG_ID", nullable = false)
private Integer contractId;
@Column(name = "POSITIONSNUMMER", nullable = false)
private int positionNumber;
Debugging shows:
( https://hibernate.atlassian.net/browse/HHH-16437#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16437#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#100221- sha1:78e8bd6 )
2 years, 5 months
[JIRA] (HHH-16408) query.setMaxResults(1) with EntityGraph and @ElementCollection gives wrong result in Hibernate 6
by Fouad Almalki (JIRA)
Fouad Almalki ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiOGQ2Y2I4N2M2... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16408?atlOrigin=eyJpIjoiOGQ2Y2... ) HHH-16408 ( https://hibernate.atlassian.net/browse/HHH-16408?atlOrigin=eyJpIjoiOGQ2Y2... ) query.setMaxResults(1) with EntityGraph and @ElementCollection gives wrong result in Hibernate 6 ( https://hibernate.atlassian.net/browse/HHH-16408?atlOrigin=eyJpIjoiOGQ2Y2... )
Change By: Fouad Almalki ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Given:
{code:java}@Table(name = "users")
@Entity
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
Integer id;
@NotNull
@Column(name = "username")
String username;
@ElementCollection
@CollectionTable(name = "users_roles_mapping", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "role_name")
Set<String> roles;
// ...
}{code}
{code:sql}-- MSSQL 2022
CREATE TABLE users (
id INT NOT NULL IDENTITY,
username VARCHAR(50) NOT NULL,
CONSTRAINT pk_users PRIMARY KEY (id)
);
CREATE TABLE roles (
name VARCHAR(50) NOT NULL,
CONSTRAINT pk_roles PRIMARY KEY (name)
);
CREATE TABLE users_roles_mapping (
user_id INT NOT NULL,
role_name VARCHAR(50) NOT NULL,
CONSTRAINT pk_users_roles_mapping PRIMARY KEY (user_id, role_name),
CONSTRAINT fk_users_roles_mapping_user FOREIGN KEY (user_id) REFERENCES users (id),
CONSTRAINT fk_users_roles_mapping_role FOREIGN KEY (role_name) REFERENCES roles (name)
);
INSERT INTO users (username) VALUES ('user1');
INSERT INTO roles (name) VALUES ('role1'), ('role2');
INSERT INTO users_roles_mapping (user_id, role_name) VALUES (1, 'role1'), (1, 'role2');{code}
{code:java}public Set<String> getUserRoles() {
EntityGraph<UserEntity> entityGraph = entityManager.createEntityGraph(UserEntity.class);
entityGraph.addAttributeNodes("roles");
Query query = entityManager.createQuery("SELECT u FROM UserEntity u WHERE u.id = 1");
query.setMaxResults(1);
query.setHint( EntityGraphType "jakarta. FETCH persistence. getKey() fetchgraph" , entityGraph);
UserEntity userEntity = (UserEntity) query.getSingleResult();
return userEntity.getRoles();
}{code}
{{getUserRoles}} returns 2 roles in Hibernate {{5.6.15.Final}} and returns 1 role (which is wrong) in Hibernate {{6.1.7}}.
( https://hibernate.atlassian.net/browse/HHH-16408#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16408#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#100221- sha1:89dc9bf )
2 years, 5 months
[JIRA] (HHH-16435) Discriminators are completely ignored when Filter is active
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiODI5YzE3Y2My... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16435?atlOrigin=eyJpIjoiODI5Yz... ) HHH-16435 ( https://hibernate.atlassian.net/browse/HHH-16435?atlOrigin=eyJpIjoiODI5Yz... ) Discriminators are completely ignored when Filter is active ( https://hibernate.atlassian.net/browse/HHH-16435?atlOrigin=eyJpIjoiODI5Yz... )
Change By: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Given
* A polymorphic single table entity using column discriminator
* One persisted instance of this entity
* Another persisted instance using another subclass/discriminator
* A simple Filter is defined
When
* Activating the filter and executing a deletion for all entities from a single discriminator
Then
* All entities, regardless their discriminator, are deleted
{code:java}@Entity
@Table(name = "ENTITY_A")
@DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER)
@FilterDef(name = "DUMMY_FILTER", defaultCondition = "(ID IS NOT NULL)")
@Filter(name = "DUMMY_FILTER")
public abstract class AbstractEntityA {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
Integer id;
}{code}
{code:java}@Entity
@DiscriminatorValue("1")
public class EntityAChildOne extends AbstractEntityA {}{code}
{code:java}@Entity
@DiscriminatorValue("2")
public class EntityAChildTwo extends AbstractEntityA {}{code}
{code:java} @Test
public void hhhXXXXTest() throws Exception {
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
EntityAChildOne entityAChildOne = new EntityAChildOne();
EntityAChildTwo entityAChildTwo = new EntityAChildTwo();
s.persist(entityAChildOne);
s.persist(entityAChildTwo);
tx.commit();
}
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
s.enableFilter("DUMMY_FILTER");
MutationQuery deleteQuery = s.createMutationQuery("delete from org.hibernate.bugs.EntityAChildTwo");
int actual = deleteQuery.executeUpdate();
assertThat(actual).isEqualTo(1);
tx.commit();
Query<AbstractEntityA> query = s.createQuery("select c from org.hibernate.bugs.AbstractEntityA c",
AbstractEntityA.class);
List<AbstractEntityA> actualList = query.list();
assertThat(actualList).hasSize(1);
}
}{code}
{noformat}Hibernate:
delete
from
ENTITY_A
where
(
ID IS NOT NULL
){noformat}
{noformat}org.opentest4j.AssertionFailedError:
Expecting:
<2>
to be equal to:
<1>
but was not.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at org.hibernate.bugs.ORMUnitTestCase.hhhXXXXTest(ORMUnitTestCase.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833){noformat}
Not enabling the filter will work as expected, only deleting {{EntityAChildTwo}}
Enabling the filter will ignore completely the discriminator and delete everything in that table.
A failing test case is to be attached and also available here [https://github. com/ratoaq2/HHH-16435|https://github.com/ratoaq2/HHH-16435|smart-link]
( https://hibernate.atlassian.net/browse/HHH-16435#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16435#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#100221- sha1:89dc9bf )
2 years, 5 months
[JIRA] (HHH-16435) Discriminators are completely ignored when Filter is active
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMjAwMTJjMDY2... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16435?atlOrigin=eyJpIjoiMjAwMT... ) HHH-16435 ( https://hibernate.atlassian.net/browse/HHH-16435?atlOrigin=eyJpIjoiMjAwMT... ) Discriminators are completely ignored when Filter is active ( https://hibernate.atlassian.net/browse/HHH-16435?atlOrigin=eyJpIjoiMjAwMT... )
Issue Type: Bug Affects Versions: 6.2.0 Assignee: Unassigned Created: 04/Apr/2023 10:39 AM Priority: Critical Reporter: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Given
* A polymorphic single table entity using column discriminator
* One persisted instance of this entity
* Another persisted instance using another subclass/discriminator
* A simple Filter is defined
When
* Activating the filter and executing a deletion for all entities from a single discriminator
Then
* All entities, regardless their discriminator, are deleted
@Entity
@Table(name = "ENTITY_A" )
@DiscriminatorColumn(name = "DISC_COL" , discriminatorType = DiscriminatorType.INTEGER)
@FilterDef(name = "DUMMY_FILTER" , defaultCondition = "(ID IS NOT NULL)" )
@Filter(name = "DUMMY_FILTER" )
public abstract class AbstractEntityA {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID" )
Integer id;
}
@Entity
@DiscriminatorValue( "1" )
public class EntityAChildOne extends AbstractEntityA {}
@Entity
@DiscriminatorValue( "2" )
public class EntityAChildTwo extends AbstractEntityA {}
@Test
public void hhhXXXXTest() throws Exception {
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
EntityAChildOne entityAChildOne = new EntityAChildOne();
EntityAChildTwo entityAChildTwo = new EntityAChildTwo();
s.persist(entityAChildOne);
s.persist(entityAChildTwo);
tx.commit();
}
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
s.enableFilter( "DUMMY_FILTER" );
MutationQuery deleteQuery = s.createMutationQuery( "delete from org.hibernate.bugs.EntityAChildTwo" );
int actual = deleteQuery.executeUpdate();
assertThat(actual).isEqualTo(1);
tx.commit();
Query<AbstractEntityA> query = s.createQuery( "select c from org.hibernate.bugs.AbstractEntityA c" ,
AbstractEntityA.class);
List<AbstractEntityA> actualList = query.list();
assertThat(actualList).hasSize(1);
}
}
Hibernate:
delete
from
ENTITY_A
where
(
ID IS NOT NULL
)
org.opentest4j.AssertionFailedError:
Expecting:
<2>
to be equal to:
<1>
but was not.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at org.hibernate.bugs.ORMUnitTestCase.hhhXXXXTest(ORMUnitTestCase.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833)
Not enabling the filter will work as expected, only deleting EntityAChildTwo
Enabling the filter will ignore completely the discriminator and delete everything in that table.
A failing test case is to be attached.
( https://hibernate.atlassian.net/browse/HHH-16435#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16435#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#100221- sha1:89dc9bf )
2 years, 5 months
[JIRA] (HHH-16434) @Where condition duplicated and join is not left join when using EntityGraph
by Stefan Seidel (JIRA)
Stefan Seidel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=6070150... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiN2U2MzA2NGY2... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16434?atlOrigin=eyJpIjoiN2U2Mz... ) HHH-16434 ( https://hibernate.atlassian.net/browse/HHH-16434?atlOrigin=eyJpIjoiN2U2Mz... ) @Where condition duplicated and join is not left join when using EntityGraph ( https://hibernate.atlassian.net/browse/HHH-16434?atlOrigin=eyJpIjoiN2U2Mz... )
Issue Type: Bug Affects Versions: 6.2.0.CR4 Assignee: Unassigned Created: 04/Apr/2023 06:27 AM Priority: Major Reporter: Stefan Seidel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=6070150... )
Test Case: https://github.com/s-seidel/hibernate-test-case-templates/tree/entity-gra...
Test: https://github.com/s-seidel/hibernate-test-case-templates/blob/5a84472593...
The SQL query that is generated is:
select
e1_0. "id" ,
c1_0. "employeeid" ,
c1_0. "skillid" ,
s1_0. "id" ,
c1_0. " end " ,
c1_0. " start "
from
"employee" e1_0
left join
"skillassignment" c1_0
on e1_0. "id" =c1_0. "employeeid"
and (
c1_0. " start " <= cast ( current_timestamp as date )
and (
c1_0. " end " >= cast ( current_timestamp as date )
or c1_0. " end " is null
)
)
and (
c1_0. " start " <= cast ( current_timestamp as date )
and (
c1_0. " end " >= cast ( current_timestamp as date )
or c1_0. " end " is null
)
)
join
"skill" s1_0
on s1_0. "id" =c1_0. "skillid"
But the expected (and in Hibernate 5, actual) Query should be:
select
e1_0. "id" ,
c1_0. "employeeid" ,
c1_0. "skillid" ,
s1_0. "id" ,
c1_0. " end " ,
c1_0. " start "
from
"employee" e1_0
left join
"skillassignment" c1_0
on e1_0. "id" =c1_0. "employeeid"
and (
c1_0. " start " <= cast ( current_timestamp as date )
and (
c1_0. " end " >= cast ( current_timestamp as date )
or c1_0. " end " is null
)
)
LEFT join
"skill" s1_0
on s1_0. "id" =c1_0. "skillid"
So the where clause is duplicated and the join to skill is not a LEFT join anymore, leading to too few results - the idea is that skillassignment is optional, therefore skill must also be optional.
( https://hibernate.atlassian.net/browse/HHH-16434#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16434#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#100221- sha1:89dc9bf )
2 years, 5 months