From c76859709af26065f67a0d062a68b9040ebef90c Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Mon, 2 Dec 2013 12:55:42 -0500 Subject: [PATCH] HHH-8701 - Fix JPQL TYPE operator test Conflicts: hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/QueryTest.java --- .../hibernate/ejb/test/query/Contractor.java | 69 +++++++++++ .../hibernate/ejb/test/query/Employee.java | 107 ++++++++++++++++++ .../hibernate/ejb/test/query/QueryTest.java | 36 +++--- 3 files changed, 195 insertions(+), 17 deletions(-) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Contractor.java create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Employee.java diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Contractor.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Contractor.java new file mode 100644 index 0000000000..959613a2c5 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Contractor.java @@ -0,0 +1,69 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.test.query; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +@DiscriminatorValue("Contractor") +public class Contractor extends Employee { + private String company; + + public Contractor() { + } + + public Contractor(String name, Double salary, String company) { + super( name, salary ); + this.company = company; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( !( o instanceof Contractor ) ) return false; + if ( !super.equals( o ) ) return false; + + Contractor that = (Contractor) o; + + if ( company != null ? !company.equals( that.company ) : that.company != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (company != null ? company.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Contractor(" + super.toString() + ", company = " + company + ")"; + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Employee.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Employee.java new file mode 100644 index 0000000000..c60c414054 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/Employee.java @@ -0,0 +1,107 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.test.query; + +import javax.persistence.DiscriminatorColumn; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import java.io.Serializable; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn(name = "Employee") +public class Employee implements Serializable { + @Id + @GeneratedValue + private Long id; + + private String name; + + private Double salary; + + public Employee() { + } + + public Employee(String name, Double salary) { + this.name = name; + this.salary = salary; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( !( o instanceof Employee ) ) return false; + + Employee employee = (Employee) o; + + if ( id != null ? !id.equals( employee.id ) : employee.id != null ) return false; + if ( name != null ? !name.equals( employee.name ) : employee.name != null ) return false; + if ( salary != null ? !salary.equals( employee.salary ) : employee.salary != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (salary != null ? salary.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Employee(id = " + id + ", name = " + name + ", salary = " + salary + ")"; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getSalary() { + return salary; + } + + public void setSalary(Double salary) { + this.salary = salary; + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java index fafd4557d8..0ba818c30b 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/query/QueryTest.java @@ -23,28 +23,28 @@ */ package org.hibernate.ejb.test.query; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; + import javax.persistence.EntityManager; import javax.persistence.Query; import javax.persistence.TemporalType; import javax.persistence.Tuple; -import org.junit.Test; - import org.hibernate.Hibernate; import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.ejb.test.Distributor; import org.hibernate.ejb.test.Item; import org.hibernate.ejb.test.Wallet; import org.hibernate.testing.TestForIssue; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.Test; /** * @author Emmanuel Bernard @@ -107,18 +107,18 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase { em.close(); } + @Test public void testTypeExpression() throws Exception { - EntityManager em = getOrCreateEntityManager(); + final EntityManager em = getOrCreateEntityManager(); em.getTransaction().begin(); - Item item = new Item( "Mouse", "Micro$oft mouse" ); - em.persist( item ); - item = new Item( "Computer", "Apple II" ); - em.persist( item ); - Query q = em.createQuery( "select i from Item i where TYPE(i) = :itemType" ); - q.setParameter( "itemType", Item.class ); - List result = q.getResultList(); + final Employee employee = new Employee( "Lukasz", 100.0 ); + em.persist( employee ); + final Contractor contractor = new Contractor( "Kinga", 100.0, "Microsoft" ); + em.persist( contractor ); + final Query q = em.createQuery( "SELECT e FROM Employee e where TYPE(e) <> Contractor" ); + final List result = q.getResultList(); assertNotNull( result ); - assertEquals( 2, result.size() ); + assertEquals( Arrays.asList( employee ), result ); em.getTransaction().rollback(); em.close(); } @@ -635,7 +635,9 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase { return new Class[]{ Item.class, Distributor.class, - Wallet.class + Wallet.class, + Contractor.class, + Employee.class }; } }