[JIRA] (HHH-16986) CoercionException caused by attempted coercion of query param to entity field type
by Ruben Gehring (JIRA)
Ruben Gehring ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=6401f5e... ) *commented* on HHH-16986 ( https://hibernate.atlassian.net/browse/HHH-16986?atlOrigin=eyJpIjoiN2U0Yz... )
Re: CoercionException caused by attempted coercion of query param to entity field type ( https://hibernate.atlassian.net/browse/HHH-16986?atlOrigin=eyJpIjoiN2U0Yz... )
I think we’re looking at it from different perspectives.
You seem to be looking at it from a perspective of “how can we minimize the amount of undefined behavior hibernate produces”.
I’m looking at it from a perspective of “If I pass correct values (and types) to Hibernate I’d like to get correct results”.
In the case of multiplication of with 1+ unknown values unspecified behavior could occur, depending on the specific types. For the types int and double the list sort of looks like this:
* Valid computations:
* (int, int) → int
* (int, int) → double
* (double, int) → double (and the symmetric case)
* (double, double) → double
* Invalid / undefined computations:
* (double, int) → int
* (double, double) → int
When hibernate cannot (feasibly?) know which of these cases it’s in you’d prefer to err on the side of “excluding undefined behavior”, that is ideally all of these throw an exception. At least that’s my impression. And honestly, that sounds pretty good?
But I guess a way to do math in HQL is desirable? In which case there needs to be a way to communicate the unknown types to Hibernate so that it can know it’s in a valid case and does not error. I think your proposal is to have something akin to an explicit type cast be part of the query?
On first glance it seems to be me that the information is already present in a stronger the form of the parameter type. Contrast the following examples:
// Version 1
Object value = "foo" ;
session.createQuery( "... SET test.bigDecimalField = test.intField * cast (?1 as int ) FROM ..." )
.setParameter(1, value)...
// Version 2
int value = 1;
session.createQuery( "... SET test.bigDecimalField = test.intField * ?1 FROM ..." )
.setParameter(1, value)...
// Version 3
int value = 1;
session.createQuery( "... SET test.bigDecimalField = test.intField * cast (?1 as int ) FROM ..." )
.setParameter(1, value)...
My impression is that version 3 is what you’re thinking but it seems not obviously better to me than version 2. Perhaps it is, you’re certainly more familiar with the topic than I am. But let me lay out my thinking:
Version 1 and 3 communicate the intended type of ?1 in the query but as version 1 demonstrates that is not reliable, as I can write any type I want in the query string.
Version 2 and 3 communicate the intended type of ?1 via the type of value which is enforced by the java typing system. Not perfectly reliable perhaps but more reliable than version 1 for it seems to me.
Given that, what does version 3 add that version 2 does not provide? Requiring cast(?1 as double) when ?1 is of type int seems weaker than requiring the java-type of value to be double and requiring both seems redundant to me.
( https://hibernate.atlassian.net/browse/HHH-16986#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16986#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#100233- sha1:f5b6255 )
2 years, 5 months
[JIRA] (HHH-16936) Entity not being removed from persistence context after a delete in JPQL
by Andrea Boriero (JIRA)
Andrea Boriero ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *commented* on HHH-16936 ( https://hibernate.atlassian.net/browse/HHH-16936?atlOrigin=eyJpIjoiNzRhZG... )
Re: Entity not being removed from persistence context after a delete in JPQL ( https://hibernate.atlassian.net/browse/HHH-16936?atlOrigin=eyJpIjoiNzRhZG... )
Hi Jean-Baptiste Hainaut ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=712020%... ) ,
I tried to to create a reproducer for the issue but without any success.
@Jpa(
annotatedClasses = {
EntityDeleteTest.SchoolSetting.class
}
)
public class EntityDeleteTest {
static String SCHOOL_ID = "MY_SCHOOL";
static String SCHOOL_KEY = "ABSENCE_FOR_DAY_OFF";
@Test
public void testIt(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
SchoolSetting schoolSetting = new SchoolSetting( SCHOOL_ID, SCHOOL_KEY, true );
entityManager.persist( schoolSetting );
}
);
scope.inTransaction(
entityManager -> {
SchoolSettingId primaryKey = new SchoolSettingId( SCHOOL_ID, SCHOOL_KEY );
SchoolSetting schoolSetting = entityManager.find(
SchoolSetting.class,
primaryKey
);
entityManager.remove( schoolSetting );
schoolSetting = entityManager.find(
SchoolSetting.class,
primaryKey
);
assertNull( schoolSetting );
}
);
}
@Test
public void testIt2(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
SchoolSetting schoolSetting = new SchoolSetting( SCHOOL_ID, SCHOOL_KEY, true );
entityManager.persist( schoolSetting );
}
);
scope.inTransaction(
entityManager -> {
Query query = entityManager.createQuery( "delete from SchoolSetting s WHERE s.id.key = :id" );
query.setParameter( "id", SCHOOL_KEY );
query.executeUpdate();
SchoolSetting schoolSetting = entityManager.find(
SchoolSetting.class,
new SchoolSettingId(
SCHOOL_ID,
SCHOOL_KEY
)
);
assertNull( schoolSetting );
}
);
}
@Entity(name = "SchoolSetting")
public static class SchoolSetting {
@EmbeddedId
private SchoolSettingId id;
private boolean activated;
public SchoolSetting() {
}
public SchoolSetting(
final String schoolId,
final String key,
final boolean activated) {
this.id = new SchoolSettingId( schoolId, key );
this.activated = activated;
}
public String getKey() {
return this.id.getKey();
}
}
@Embeddable
public static class SchoolSettingId implements Serializable {
private String schoolId;
@Column(name = "school_key")
private String key;
public SchoolSettingId() {
}
public SchoolSettingId(
final String schoolId,
final String key) {
this.schoolId = schoolId;
this.key = key;
}
public String getSchoolId() {
return schoolId;
}
public String getKey() {
return key;
}
}
}
Can you please provide a complete reproducer?
Thanks
( https://hibernate.atlassian.net/browse/HHH-16936#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16936#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#100233- sha1:f5b6255 )
2 years, 5 months