-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-8891 - Added test for issue (no fix required)
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...ate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/WrappedEntityCriteriaTest.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,112 @@ | ||
/* | ||
* 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.orm.test.jpa.criteria; | ||
|
||
import org.hibernate.testing.jdbc.SQLStatementInspector; | ||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
import org.hibernate.testing.orm.junit.Jira; | ||
import org.hibernate.testing.orm.junit.Jpa; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.Root; | ||
|
||
/** | ||
* @author Jan Schatteman | ||
*/ | ||
@Jpa( | ||
annotatedClasses = { WrappedEntityCriteriaTest.SimpleEntity.class, WrappedEntityCriteriaTest.SimpleEntityWrapper.class }, | ||
useCollectingStatementInspector = true | ||
) | ||
@Jira( value = "https://hibernate.atlassian.net/browse/HHH-8891") | ||
public class WrappedEntityCriteriaTest { | ||
|
||
@BeforeEach | ||
public void setup(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
SimpleEntity instance = new SimpleEntity("John"); | ||
entityManager.persist( instance ); | ||
instance = new SimpleEntity("Jack"); | ||
entityManager.persist( instance ); | ||
instance = new SimpleEntity("James"); | ||
entityManager.persist( instance ); | ||
instance = new SimpleEntity("Bill"); | ||
entityManager.persist( instance ); | ||
instance = new SimpleEntity("Harry"); | ||
entityManager.persist( instance ); | ||
} | ||
); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> entityManager.createQuery( "delete from SimpleEntity" ).executeUpdate() | ||
); | ||
} | ||
|
||
@Test | ||
public void test(EntityManagerFactoryScope scope) { | ||
scope.inEntityManager( | ||
entityManager -> { | ||
SQLStatementInspector sqlInspector = scope.getCollectingStatementInspector(); | ||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); | ||
CriteriaQuery<SimpleEntityWrapper> query = criteriaBuilder.createQuery( SimpleEntityWrapper.class); | ||
Root<SimpleEntity> from = query.from(SimpleEntity.class); | ||
query.select(criteriaBuilder.construct( SimpleEntityWrapper.class, from)); | ||
// the following should only execute 1 queries (as oposed to 1+n) | ||
sqlInspector.clear(); | ||
entityManager.createQuery( query).getResultList(); | ||
sqlInspector.assertExecutedCount( 1 ); | ||
sqlInspector.assertIsSelect( 0 ); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "SimpleEntity") | ||
public static class SimpleEntity { | ||
@Id | ||
@GeneratedValue | ||
private Integer id; | ||
private String name; | ||
protected SimpleEntity() { | ||
} | ||
public SimpleEntity(String name) { | ||
this.name = name; | ||
} | ||
public Integer getId() { | ||
return id; | ||
} | ||
protected void setId(Integer id) { | ||
this.id = id; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
public static class SimpleEntityWrapper { | ||
private final SimpleEntity instance; | ||
public SimpleEntityWrapper(SimpleEntity instance) { | ||
this.instance = instance; | ||
} | ||
public SimpleEntity getBrand() { | ||
return instance; | ||
} | ||
} | ||
|
||
} |