|
| 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.test.hql; |
| 8 | + |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +import javax.persistence.Entity; |
| 12 | +import javax.persistence.GeneratedValue; |
| 13 | +import javax.persistence.Id; |
| 14 | + |
| 15 | +import org.hibernate.Session; |
| 16 | +import org.hibernate.query.Query; |
| 17 | + |
| 18 | +import org.hibernate.testing.TestForIssue; |
| 19 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +public class DeleteWhereFunctionCallTest extends BaseCoreFunctionalTestCase { |
| 24 | + |
| 25 | + @Override |
| 26 | + protected Class[] getAnnotatedClasses() { |
| 27 | + return new Class[] { |
| 28 | + SuperType.class, |
| 29 | + SubType.class |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected boolean isCleanupTestDataRequired() { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + @Before |
| 39 | + public void initData() { |
| 40 | + inTransaction( s -> { |
| 41 | + s.persist( new SuperType( -1 ) ); |
| 42 | + s.persist( new SubType( -2 ) ); |
| 43 | + } ); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + @TestForIssue(jiraKey = "HHH-14814") |
| 48 | + public void testDeleteWhereTypeFunctionCall() { |
| 49 | + inTransaction( s -> { |
| 50 | + assertThat( count( s, SuperType.class ) ).isEqualTo( 2 ); |
| 51 | + assertThat( count( s, SubType.class ) ).isEqualTo( 1 ); |
| 52 | + } ); |
| 53 | + inTransaction( s -> { |
| 54 | + Query<?> query = s.createQuery( "delete from " + SuperType.class.getName() + " e" |
| 55 | + + " where type( e ) = :type" ); |
| 56 | + query.setParameter( "type", SuperType.class ); |
| 57 | + query.executeUpdate(); |
| 58 | + } ); |
| 59 | + inTransaction( s -> { |
| 60 | + assertThat( count( s, SuperType.class ) ).isEqualTo( 1 ); |
| 61 | + assertThat( count( s, SubType.class ) ).isEqualTo( 1 ); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDeleteWhereAbsFunctionCall() { |
| 67 | + inTransaction( s -> { |
| 68 | + assertThat( count( s, SuperType.class ) ).isEqualTo( 2 ); |
| 69 | + assertThat( count( s, SubType.class ) ).isEqualTo( 1 ); |
| 70 | + } ); |
| 71 | + inTransaction( s -> { |
| 72 | + Query<?> query = s.createQuery( "delete from " + SuperType.class.getName() + " e" |
| 73 | + + " where abs( e.someNumber ) = :number" ); |
| 74 | + query.setParameter( "number", 2 ); |
| 75 | + query.executeUpdate(); |
| 76 | + } ); |
| 77 | + inTransaction( s -> { |
| 78 | + assertThat( count( s, SuperType.class ) ).isEqualTo( 1 ); |
| 79 | + assertThat( count( s, SubType.class ) ).isEqualTo( 0 ); |
| 80 | + } ); |
| 81 | + } |
| 82 | + |
| 83 | + private <T> long count(Session s, Class<T> entityClass) { |
| 84 | + return s.createQuery( "select count(e) from " + entityClass.getName() + " e", Long.class ) |
| 85 | + .uniqueResult(); |
| 86 | + } |
| 87 | + |
| 88 | + @Entity(name = "supert") |
| 89 | + public static class SuperType { |
| 90 | + @Id |
| 91 | + @GeneratedValue |
| 92 | + private Long id; |
| 93 | + |
| 94 | + private int someNumber; |
| 95 | + |
| 96 | + public SuperType() { |
| 97 | + } |
| 98 | + |
| 99 | + public SuperType(int someNumber) { |
| 100 | + this.someNumber = someNumber; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Entity(name = "subt") |
| 105 | + public static class SubType extends SuperType { |
| 106 | + public SubType() { |
| 107 | + } |
| 108 | + |
| 109 | + public SubType(int someNumber) { |
| 110 | + super( someNumber ); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments