<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<base href="https://hibernate.atlassian.net">
<title>Message Title</title>
</head>
<body class="jira" style="color: #333333; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.429">
<table id="background-table" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f5f5f5; border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt" bgcolor="#f5f5f5">
<!-- header here -->
<tbody>
<tr>
<td id="header-pattern-container" style="padding: 0px; border-collapse: collapse; padding: 10px 20px">
<table id="header-pattern" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tbody>
<tr>
<td id="header-avatar-image-container" valign="top" style="padding: 0px; border-collapse: collapse; vertical-align: top; width: 32px; padding-right: 8px" width="32"> <img id="header-avatar-image" class="image_fix" src="cid:jira-generated-image-avatar-d0106a54-eeb6-47b9-9cf3-0c8c45f0601f" height="32" width="32" border="0" style="border-radius: 3px; vertical-align: top"> </td>
<td id="header-text-container" valign="middle" style="padding: 0px; border-collapse: collapse; vertical-align: middle; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px"> <a class="user-hover" rel="mtf90" id="email_mtf90" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=mtf90" style="color:#6c797f;; color: #3b73af; text-decoration: none">M F</a> <strong>updated</strong> an issue </td>
</tr>
</tbody>
</table> </td>
</tr>
<tr>
<td id="email-content-container" style="padding: 0px; border-collapse: collapse; padding: 0 20px">
<table id="email-content-table" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; border-spacing: 0; border-collapse: separate">
<tbody>
<tr>
<!-- there needs to be content in the cell for it to render in some clients -->
<td class="email-content-rounded-top mobile-expand" style="padding: 0px; border-collapse: collapse; color: #ffffff; padding: 0 15px 0 16px; height: 15px; background-color: #ffffff; border-left: 1px solid #cccccc; border-top: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom: 0; border-top-right-radius: 5px; border-top-left-radius: 5px; height: 10px; line-height: 10px; padding: 0 15px 0 16px; mso-line-height-rule: exactly" height="10" bgcolor="#ffffff"> </td>
</tr>
<tr>
<td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff" bgcolor="#ffffff">
<table class="page-title-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tbody>
<tr>
<td class="page-title-pattern-first-line " style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; padding-top: 10px"> <a href="https://hibernate.atlassian.net/browse/HHH" style="color: #3b73af; text-decoration: none">Hibernate ORM</a> / <a href="https://hibernate.atlassian.net/browse/HHH-10488" style="color: #3b73af; text-decoration: none"><img src="cid:jira-generated-image-avatar-0a928ebb-e757-46cc-9d7d-8a8eff67283f" height="16" width="16" border="0" align="absmiddle" alt="Bug" style="vertical-align: text-bottom"></a> <a href="https://hibernate.atlassian.net/browse/HHH-10488" style="color: #3b73af; text-decoration: none">HHH-10488</a> </td>
</tr>
<tr>
<td style="vertical-align: top;; padding: 0px; border-collapse: collapse; padding-right: 5px; font-size: 20px; line-height: 30px; mso-line-height-rule: exactly" class="page-title-pattern-header-container"> <span class="page-title-pattern-header" style="font-family: Arial, sans-serif; padding: 0; font-size: 20px; line-height: 30px; mso-text-raise: 2px; mso-line-height-rule: exactly; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/HHH-10488" style="color: #3b73af; text-decoration: none">Exception when using the Treat-Operator with MapJoins</a> </span> </td>
</tr>
</tbody>
</table> </td>
</tr>
<tr>
<td class="email-content-main mobile-expand wrapper-special-margin" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff; padding-top: 10px; padding-bottom: 5px" bgcolor="#ffffff">
<table class="keyvalue-table" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tbody>
<tr>
<th style="color: #707070; font: normal 14px/20px Arial, sans-serif; text-align: left; vertical-align: top; padding: 2px 0">Change By:</th>
<td style="padding: 0px; border-collapse: collapse; font: normal 14px/20px Arial, sans-serif; padding: 2px 0 2px 5px; vertical-align: top"> <a class="user-hover" rel="mtf90" id="email_mtf90" href="https://hibernate.atlassian.net/secure/ViewProfile.jspa?name=mtf90" style="color:#6c797f;; color: #3b73af; text-decoration: none">M F</a> </td>
</tr>
</tbody>
</table> </td>
</tr>
<tr>
<td class="email-content-main mobile-expand issue-description-container" style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff; padding-top: 5px; padding-bottom: 10px" bgcolor="#ffffff">
<table class="text-paragraph-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 2px">
<tbody>
<tr>
<td class="text-paragraph-pattern-container mobile-resize-text " style="padding: 0px; border-collapse: collapse; padding: 0 0 10px 0"> <span class="diffcontext">I am experiencing some buggy behaviour regarding the TREAT operator on MapJoins. In the following I will only sketch the scenario, for the full detail see the attached maven project.<br><br>I have an entity<br><br>{code:java}<br>class DBObject {<br> private Map<DBKey, DBValue> properties;<br>}<br>{code}<br><br>that holds a mapping from keys (entities) to values (also entities). DBValue is an abstract super class for several subclasses that store a specific value, e.g. DBValueWithString or DBValueWithReference (which references other DBObjects).<br><br>I now want to query for objects, that own specific properties with certain values. Therefore I use the following CriteriaQuery:<br><br>{code:java}<br>final CriteriaBuilder builder = em.getCriteriaBuilder();<br>final CriteriaQuery<DBObject> query = builder.createQuery(DBObject.class);<br><br>final Root<DBObject> from = query.from(DBObject.class);<br>final MapJoin<DBObject, DBKey, DBValue> valueJoin = from.join(DBObject_.properties);<br><br>final MapJoin<DBObject, DBKey, DBValueWithString> stringJoin = builder.treat(valueJoin, DBValueWithString.class);<br>final Predicate predicate1 = builder.and(<br> builder.equal(stringJoin.get(DBValueWithString_.key), key1),<br> builder.equal(stringJoin.get(DBValueWithString_.value), "test")<br>);<br>{code}<br><br>This query generates to reasonable JPQL:<br><br>{noformat}<br>select<br> generatedAlias0<br> from<br> DBObject as generatedAlias0<br> inner join<br> generatedAlias0.properties as generatedAlias1<br> where<br> (<br> treat(generatedAlias1 as de.mtf.DBValueWithString).key=:param0<br> )<br> and (<br> treat(generatedAlias1 as de.mtf.DBValueWithString).value=:param1<br> )<br>{noformat}<br><br>The corresponding SQL seems a little off, however it works:<br><br>{noformat}<br>select<br> dbobject0_.id as id1_1_<br> from<br> DBObject dbobject0_<br> inner join<br> DBValue properties1_<br> on dbobject0_.id=properties1_.object_id<br> inner join<br> DBValueWithString properties1_1_<br> on properties1_.id=properties1_1_.id<br> left outer join<br> DBValueWithReference properties1_2_<br> on properties1_.id=properties1_2_.id<br> where<br> properties1_.key_id=?<br> and properties1_1_.value=?<br>{noformat}<br><br>Note the unnecessary join with DBValueWithReference. According to the doc, the TREAT operator should also filter out the non matching subtypes, therefore entities of type DBValueWithReference can be completely ignored at this point.<br><br>The problem now arises, when querying for DBValueWithReferences. The JPQL query seems fine once again:<br><br>{noformat}<br>select<br> generatedAlias0<br> from<br> DBObject as generatedAlias0<br> inner join<br> generatedAlias0.properties as generatedAlias1<br> where<br> (<br> treat(generatedAlias1 as de.mtf.DBValueWithReference).key=:param0<br> )<br> and (<br> treat(generatedAlias1 as de.mtf.DBValueWithReference).value=:param1<br> )<br>{noformat}<br><br>However the translation process to SQL fails with the following error message:<br><br>{noformat}<br>java.lang.IllegalArgumentException: Parameter value [de.mtf.DBObject@7bf9b098] did not match expected type [java.lang.String (n/a)]<br> at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:897)<br> at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:61)<br> at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:235)<br> at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638)<br> at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163)<br> at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)<br> at org.hibernate.jpa.criteria.compile.CriteriaCompiler$1$1.bind(CriteriaCompiler.java:109)<br> at org.hibernate.jpa.criteria.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:369)<br> at org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:130)<br> at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:699)<br> at de.mtf.TreatTest.testTreat(TreatTest.java:98)<br>{noformat}<br><br>The problem seems to correlate with the unnecessary join, as the type seems to be inferred solely based on the first join (with DBValueWithString), which is unnecessarily part of the second query.<br><br>It seems, DBValue is joined with every sub class that is encountered in the persistence.xml. If you uncomment the @Entity annotation for DBValueWithBoolean, it will occur in the list of join statements.<br><br>Moreover, the order of the joins seems to depend on the order of which the classes are listed in the persistence.xml. If you swap the entries for DBValueWithString and DBValueWithReference, the second query executes just fine, whereas the first query now yields:<br><br>{noformat}<br>java.lang.IllegalArgumentException: Parameter value [test] did not match expected type [de.mtf.DBObject (n/a)]<br> at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:897)<br> at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:61)<br> at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:235)<br> at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638)<br> at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163)<br> at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)<br> at org.hibernate.jpa.criteria.compile.CriteriaCompiler$1$1.bind(CriteriaCompiler.java:109)<br> at org.hibernate.jpa.criteria.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:369)<br> at org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:130)<br> at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:699)<br> at de.mtf.TreatTest.testTreat(TreatTest.java:96)<br>{noformat}<br><br>So is this just a misinterpretation of what the TREAT operator is supposed to do, or is it actually a bug in the SQL generation?<br><br>*</span> <span class="diffaddedchars" style="background-color:#ddfade;">**</span> <span class="diffcontext">EDIT*</span> <span class="diffaddedchars" style="background-color:#ddfade;">**</span> <span class="diffcontext"><br><br>To further elaborate on this issue, I tried</span> <span class="diffremovedchars" style="background-color: #ffe7e7; text-decoration:line-through;"> </span> <span class="diffaddedchars" style="background-color:#ddfade;"> to realize the same scenario using the EclipseLink JPA provider. Thereby I realized, the code given in the example may be a little unclean. For example, I used the same Join-object in several calls to the treat function. Apparently, this function may alter the Join-object, which may cause the query to fail. I added a new test project, which hopefully gets rid of all these flaws.<br><br>Ultimately, the new test-case allows a direct comparison of Hibernate and EclipseLink (via maven profiles) which shows, that the code executes as expected using EclipseLink but fails when using Hibernate. The main issue seems to be the same as mentioned above: When performing the TREAT operation, Hibernate wrongly joins on every subtype of the superclass.</span> </td>
</tr>
</tbody>
</table> </td>
</tr>
<tr>
<td class="email-content-main mobile-expand " style="padding: 0px; border-collapse: collapse; border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 0; border-bottom: 0; padding: 0 15px 0 16px; background-color: #ffffff" bgcolor="#ffffff">
<table id="actions-pattern" cellspacing="0" cellpadding="0" border="0" width="100%" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 1px">
<tbody>
<tr>
<td id="actions-pattern-container" valign="middle" style="padding: 0px; border-collapse: collapse; padding: 10px 0 10px 24px; vertical-align: middle; padding-left: 0">
<table align="left" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tbody>
<tr>
<td class="actions-pattern-action-icon-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 0px; vertical-align: middle"> <a href="https://hibernate.atlassian.net/browse/HHH-10488#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none"> <img class="actions-pattern-action-icon-image" src="cid:jira-generated-image-static-comment-icon-bf6299dc-c66f-41b7-81ce-51694df7cb84" alt="Add Comment" title="Add Comment" height="16" width="16" border="0" style="vertical-align: middle"> </a> </td>
<td class="actions-pattern-action-text-container" style="padding: 0px; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 20px; mso-line-height-rule: exactly; mso-text-raise: 4px; padding-left: 5px"> <a href="https://hibernate.atlassian.net/browse/HHH-10488#add-comment" target="_blank" title="Add Comment" style="color: #3b73af; text-decoration: none">Add Comment</a> </td>
</tr>
</tbody>
</table> </td>
</tr>
</tbody>
</table> </td>
</tr>
<!-- there needs to be content in the cell for it to render in some clients -->
<tr>
<td class="email-content-rounded-bottom mobile-expand" style="padding: 0px; border-collapse: collapse; color: #ffffff; padding: 0 15px 0 16px; height: 5px; line-height: 5px; background-color: #ffffff; border-top: 0; border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; mso-line-height-rule: exactly" height="5" bgcolor="#ffffff"> </td>
</tr>
</tbody>
</table> </td>
</tr>
<tr>
<td id="footer-pattern" style="padding: 0px; border-collapse: collapse; padding: 12px 20px">
<table id="footer-pattern-container" cellspacing="0" cellpadding="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tbody>
<tr>
<td id="footer-pattern-text" class="mobile-resize-text" width="100%" style="padding: 0px; border-collapse: collapse; color: #999999; font-size: 12px; line-height: 18px; font-family: Arial, sans-serif; mso-line-height-rule: exactly; mso-text-raise: 2px"> This message was sent by Atlassian JIRA <span id="footer-build-information">(v7.1.0-OD-06-005#71002-<span title="1d15c989f1db2b53a95ce9e7066f4bb16c8773ac" data-commit-id="1d15c989f1db2b53a95ce9e7066f4bb16c8773ac}">sha1:1d15c98</span>)</span> </td>
<td id="footer-pattern-logo-desktop-container" valign="top" style="padding: 0px; border-collapse: collapse; padding-left: 20px; vertical-align: top">
<table style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt">
<tbody>
<tr>
<td id="footer-pattern-logo-desktop-padding" style="padding: 0px; border-collapse: collapse; padding-top: 3px"> <img id="footer-pattern-logo-desktop" src="cid:jira-generated-image-static-footer-desktop-logo-e83bcde0-b7f1-4d16-8b93-87c2c6222486" alt="Atlassian logo" title="Atlassian logo" width="169" height="36" class="image_fix"> </td>
</tr>
</tbody>
</table> </td>
</tr>
</tbody>
</table> </td>
</tr>
</tbody>
</table>
</body>
</html>