|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.inheritance; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.DiscriminatorColumn; |
| 21 | +import jakarta.persistence.Entity; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.Inheritance; |
| 24 | +import jakarta.persistence.InheritanceType; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.hibernate.orm.test.inheritance.JoinedInheritanceDiscriminatorRemovalByTypeTest.BaseEntity; |
| 28 | +import static org.hibernate.orm.test.inheritance.JoinedInheritanceDiscriminatorRemovalByTypeTest.BaseVehicle; |
| 29 | +import static org.hibernate.orm.test.inheritance.JoinedInheritanceDiscriminatorRemovalByTypeTest.Car; |
| 30 | +import static org.hibernate.orm.test.inheritance.JoinedInheritanceDiscriminatorRemovalByTypeTest.Truck; |
| 31 | + |
| 32 | +@DomainModel( annotatedClasses = { Car.class, Truck.class, BaseVehicle.class, BaseEntity.class, } ) |
| 33 | +@SessionFactory |
| 34 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17667" ) |
| 35 | +public class JoinedInheritanceDiscriminatorRemovalByTypeTest { |
| 36 | + @BeforeEach |
| 37 | + public void setUp(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( session -> { |
| 39 | + session.persist( Car.create( 1L ) ); |
| 40 | + session.persist( Car.create( 2L ) ); |
| 41 | + session.persist( Car.create( 3L ) ); |
| 42 | + session.persist( Truck.create( 4L ) ); |
| 43 | + session.persist( Truck.create( 5L ) ); |
| 44 | + } ); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + public void tearDown(SessionFactoryScope scope) { |
| 49 | + scope.inTransaction( session -> session.createMutationQuery( "delete from BaseEntity" ).executeUpdate() ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testInPredicateParameter(SessionFactoryScope scope) { |
| 54 | + scope.inTransaction( session -> session.createMutationQuery( |
| 55 | + "delete from BaseVehicle e where type(e) in(:type)" |
| 56 | + ).setParameter( "type", Car.class ).executeUpdate() ); |
| 57 | + assertRemaining( scope, 4L, 5L ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testInPredicateLiteral(SessionFactoryScope scope) { |
| 62 | + scope.inTransaction( session -> session.createMutationQuery( |
| 63 | + "delete from BaseVehicle e where type(e) in(Truck)" |
| 64 | + ).executeUpdate() ); |
| 65 | + assertRemaining( scope, 1L, 2L, 3L ); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testComparisonPredicateParameter(SessionFactoryScope scope) { |
| 70 | + scope.inTransaction( session -> session.createMutationQuery( |
| 71 | + "delete from BaseVehicle e where type(e) = :type" |
| 72 | + ).setParameter( "type", Truck.class ).executeUpdate() ); |
| 73 | + assertRemaining( scope, 1L, 2L, 3L ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testComparisonPredicateLiteral(SessionFactoryScope scope) { |
| 78 | + scope.inTransaction( session -> session.createMutationQuery( |
| 79 | + "delete from BaseVehicle e where type(e) = Car" |
| 80 | + ).executeUpdate() ); |
| 81 | + assertRemaining( scope, 4L, 5L ); |
| 82 | + } |
| 83 | + |
| 84 | + private void assertRemaining(SessionFactoryScope scope, Long... ids) { |
| 85 | + scope.inSession( session -> { |
| 86 | + final List<BaseVehicle> resultList = session.createQuery( |
| 87 | + "from BaseVehicle ", |
| 88 | + BaseVehicle.class |
| 89 | + ).getResultList(); |
| 90 | + assertThat( resultList ).hasSize( ids.length ); |
| 91 | + assertThat( resultList.stream().map( BaseEntity::getId ) ).containsOnly( ids ); |
| 92 | + } ); |
| 93 | + } |
| 94 | + |
| 95 | + @Entity( name = "Car" ) |
| 96 | + public static class Car extends BaseVehicle { |
| 97 | + public static Car create(Long id) { |
| 98 | + final Car car = new Car(); |
| 99 | + car.setId( id ); |
| 100 | + return car; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Entity( name = "Truck" ) |
| 105 | + public static class Truck extends BaseVehicle { |
| 106 | + public static Truck create(Long id) { |
| 107 | + final Truck truck = new Truck(); |
| 108 | + truck.setId( id ); |
| 109 | + return truck; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Entity( name = "BaseVehicle" ) |
| 114 | + public static abstract class BaseVehicle extends BaseEntity { |
| 115 | + } |
| 116 | + |
| 117 | + @Entity( name = "BaseEntity" ) |
| 118 | + @DiscriminatorColumn( name = "disc_col" ) |
| 119 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 120 | + public static abstract class BaseEntity implements Serializable { |
| 121 | + @Id |
| 122 | + private Long id; |
| 123 | + |
| 124 | + public Long getId() { |
| 125 | + return id; |
| 126 | + } |
| 127 | + |
| 128 | + public void setId(Long id) { |
| 129 | + this.id = id; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments