HHH-16459 Expand existing test of generic associations to ToOne associations

This commit is contained in:
Yoann Rodière 2023-04-12 11:03:14 +02:00 committed by Andrea Boriero
parent e47f262f38
commit 7d3f6188f8
2 changed files with 192 additions and 5 deletions

View File

@ -38,13 +38,13 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
@SessionFactory
@DomainModel( annotatedClasses = {
GenericAssociationTest.AbstractParent.class,
GenericAssociationTest.Parent.class,
GenericAssociationTest.AbstractChild.class,
GenericAssociationTest.Child.class
GenericToManyAssociationTest.AbstractParent.class,
GenericToManyAssociationTest.Parent.class,
GenericToManyAssociationTest.AbstractChild.class,
GenericToManyAssociationTest.Child.class
} )
@Jira( "https://hibernate.atlassian.net/browse/HHH-16378" )
public class GenericAssociationTest {
public class GenericToManyAssociationTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {

View File

@ -0,0 +1,187 @@
/*
* 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.annotations.generics;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.query.criteria.JpaPath;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Root;
/**
* @author Yoann Rodière
* @author Marco Belladelli
*/
@SessionFactory
@DomainModel( annotatedClasses = {
GenericToOneAssociationTest.AbstractParent.class,
GenericToOneAssociationTest.Parent.class,
GenericToOneAssociationTest.AbstractChild.class,
GenericToOneAssociationTest.Child.class
} )
@Jira( "https://hibernate.atlassian.net/browse/HHH-16378" )
public class GenericToOneAssociationTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final Parent parent = new Parent( 1L );
session.persist( parent );
final Child child = new Child( 2L );
child.setParent( parent );
parent.setChild( child );
session.persist( child );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "from Parent", Parent.class )
.getResultList()
.forEach( p -> p.setChild( null ) ) );
scope.inTransaction( session -> {
session.createMutationQuery( "delete from Child" ).executeUpdate();
session.createMutationQuery( "delete from Parent" ).executeUpdate();
} );
}
@Test
public void testParentQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> assertThat( session.createQuery(
"select parent.id from Child",
Long.class
).getSingleResult() ).isEqualTo( 1L ) );
}
@Test
public void testParentCriteriaQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Long> query = cb.createQuery( Long.class );
final Root<Child> root = query.from( Child.class );
final Path<Parent> parent = root.get( "parent" );
// generic attributes are always reported as Object java type
assertThat( parent.getJavaType() ).isEqualTo( Object.class );
assertThat( parent.getModel() ).isSameAs( root.getModel().getAttribute( "parent" ) );
assertThat( ( (JpaPath<?>) parent ).getResolvedModel().getBindableJavaType() ).isEqualTo( Parent.class );
final Long result = session.createQuery( query.select( parent.get( "id" ) ) ).getSingleResult();
assertThat( result ).isEqualTo( 1L );
} );
}
@Test
public void testChildQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> assertThat( session.createQuery(
"select c.id from Parent p join p.child c",
Long.class
).getSingleResult() ).isEqualTo( 2L ) );
}
@Test
public void testChildCriteriaQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<Long> query = cb.createQuery( Long.class );
final Root<Parent> root = query.from( Parent.class );
final Join<Parent, Child> join = root.join( "child" );
// generic attributes are always reported as Object java type
assertThat( join.getJavaType() ).isEqualTo( Object.class );
assertThat( join.getModel() ).isSameAs( root.getModel().getAttribute( "child" ) );
assertThat( ( (JpaPath<?>) join ).getResolvedModel().getBindableJavaType() ).isEqualTo( Child.class );
final Long result = session.createQuery( query.select( join.get( "id" ) ) ).getSingleResult();
assertThat( result ).isEqualTo( 2L );
} );
}
@MappedSuperclass
public abstract static class AbstractParent<T> {
@OneToOne
private T child;
public T getChild() {
return child;
}
public void setChild(T child) {
this.child = child;
}
}
@Entity( name = "Parent" )
public static class Parent extends AbstractParent<Child> {
@Id
private Long id;
public Parent() {
}
public Parent(Long id) {
this.id = id;
}
public Long getId() {
return this.id;
}
}
@MappedSuperclass
public abstract static class AbstractChild<T> {
@OneToOne(mappedBy = "child")
private T parent;
public AbstractChild() {
}
public abstract Long getId();
public T getParent() {
return this.parent;
}
public void setParent(T parent) {
this.parent = parent;
}
}
@Entity( name = "Child" )
public static class Child extends AbstractChild<Parent> {
@Id
protected Long id;
public Child() {
}
public Child(Long id) {
this.id = id;
}
@Override
public Long getId() {
return this.id;
}
}
}