HHH-17255 Add test for issue

This commit is contained in:
Christian Beikov 2023-09-25 11:21:52 +02:00 committed by Marco Belladelli
parent db2f2d8a9f
commit 458670b849
1 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,130 @@
/*
* 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.inheritance;
import java.util.HashMap;
import java.util.Map;
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.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import static org.assertj.core.api.Assertions.assertThat;
@DomainModel( annotatedClasses = {
MapManyToManyTreatJoinTest.JoinedBase.class,
MapManyToManyTreatJoinTest.JoinedSub1.class,
MapManyToManyTreatJoinTest.JoinedSub2.class
} )
@SessionFactory
@Jira( "https://hibernate.atlassian.net/browse/HHH-17255" )
public class MapManyToManyTreatJoinTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final JoinedSub1 o1 = new JoinedSub1( 1, 123 );
final JoinedSub2 o2 = new JoinedSub2( 2, "321" );
session.persist( o2 );
session.persist( o1 );
o1.getSubMap().put( 2, o2 );
o2.getSubMap().put( 1, o1 );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "from JoinedBase", JoinedBase.class )
.getResultList()
.forEach( session::remove ) );
}
@Test
public void testTreatQueryStringProp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final String result = session.createSelectionQuery(
"select c.stringProp from JoinedBase t join treat(t.subMap as JoinedSub2) c",
String.class
).getSingleResult();
assertThat( result ).isEqualTo( "321" );
} );
}
@Test
public void testTreatQueryIntProp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final Integer result = session.createSelectionQuery(
"select c.intProp from JoinedBase t join treat(t.subMap as JoinedSub1) c",
Integer.class
).getSingleResult();
assertThat( result ).isEqualTo( 123 );
} );
}
@Entity( name = "JoinedBase" )
@Inheritance( strategy = InheritanceType.JOINED )
public static abstract class JoinedBase {
@Id
private Integer id;
private String name;
@ManyToMany
@JoinTable( name = "sub_map" )
private Map<Integer, JoinedBase> subMap = new HashMap<>();
public JoinedBase() {
}
public JoinedBase(Integer id) {
this.id = id;
}
public Map<Integer, JoinedBase> getSubMap() {
return subMap;
}
}
@Entity( name = "JoinedSub1" )
@Table( name = "joined_sub_1" )
public static class JoinedSub1 extends JoinedBase {
private Integer intProp;
public JoinedSub1() {
}
public JoinedSub1(Integer id, Integer intProp) {
super( id );
this.intProp = intProp;
}
}
@Entity( name = "JoinedSub2" )
@Table( name = "joined_sub_2" )
public static class JoinedSub2 extends JoinedBase {
private String stringProp;
public JoinedSub2() {
}
public JoinedSub2(Integer id, String stringProp) {
super( id );
this.stringProp = stringProp;
}
}
}