forked from hibernate/hibernate-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HHH-17416] Add new inheritor JavaObjectType for specifying unresolve…
…d query parameter
- Loading branch information
1 parent
71c95f1
commit 67cdd0b
Showing
4 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
hibernate-core/src/main/java/org/hibernate/type/QueryParameterJavaObjectType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.type; | ||
|
||
public class QueryParameterJavaObjectType extends JavaObjectType { | ||
|
||
public static final QueryParameterJavaObjectType INSTANCE = new QueryParameterJavaObjectType(); | ||
|
||
public QueryParameterJavaObjectType() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "QUERY_PARAMETER_JAVA_OBJECT"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/HHH17416Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.hibernate.orm.test.query.hql; | ||
|
||
import jakarta.persistence.TypedQuery; | ||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.Root; | ||
import org.hibernate.orm.test.query.sqm.domain.Person; | ||
import org.hibernate.orm.test.query.sqm.domain.Person_; | ||
import org.hibernate.query.SelectionQuery; | ||
import org.hibernate.query.SemanticException; | ||
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@JiraKey(value = "HHH-17416") | ||
public class HHH17416Test extends BaseSessionFactoryFunctionalTest { | ||
|
||
private static final Person person = new Person(); | ||
static { | ||
person.setPk(7); | ||
person.setNickName("Tadpole"); | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] {Person.class}; | ||
} | ||
|
||
@BeforeEach | ||
public void setup() { | ||
inTransaction(session -> session.persist(person)); | ||
} | ||
|
||
@AfterEach | ||
public void teardown() { | ||
inTransaction(session -> session.createMutationQuery("delete from Person").executeUpdate()); | ||
} | ||
|
||
@Test | ||
public void testWhereClauseWithTuple() { | ||
sessionFactoryScope().inTransaction( | ||
entityManager -> { | ||
SelectionQuery<Person> selectionQuery = entityManager.createSelectionQuery("from Person p where (p.id, p.nickName) = (:val1, :val2)", Person.class); | ||
selectionQuery = selectionQuery.setParameter("val1", person.getPk()).setParameter("val2", person.getNickName()); | ||
Person retrievedPerson = selectionQuery.getSingleResult(); | ||
Assertions.assertEquals(person.getPk(), retrievedPerson.getPk()); | ||
Assertions.assertEquals(person.getNickName(), retrievedPerson.getNickName()); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testWhereClauseWithInvalidObjectType() { | ||
sessionFactoryScope().inTransaction( | ||
entityManager -> { | ||
CriteriaBuilder builder = entityManager.getCriteriaBuilder(); | ||
|
||
CriteriaQuery<Person> criteria = builder.createQuery(Person.class); | ||
Root<Person> root = criteria.from(Person.class); | ||
criteria.select(root); | ||
|
||
try { | ||
criteria.where(builder.equal(root.get(Person_.nickName), builder.literal(new Object()))); | ||
TypedQuery<Person> ignored = entityManager.createQuery(criteria); | ||
Assertions.fail("Should have failed with 'Cannot compare left expression of type' of type `org.hibernate.query.SemanticException'"); | ||
} | ||
catch (Exception e) { | ||
Assertions.assertTrue(e instanceof SemanticException); | ||
Assertions.assertTrue(e.getMessage().startsWith("Cannot compare left expression of type")); | ||
} | ||
} | ||
); | ||
} | ||
} |