Skip to content

Commit

Permalink
HHH-8891 - Added test for issue (no fix required)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
  • Loading branch information
jrenaat committed Nov 24, 2023
1 parent 7c7bf27 commit babde79
Showing 1 changed file with 112 additions and 0 deletions.
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;
}
}

}

0 comments on commit babde79

Please sign in to comment.