diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Animal.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Animal.java new file mode 100644 index 0000000000..1281f81c7a --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Animal.java @@ -0,0 +1,45 @@ +package org.hibernate.test.annotations.filter.subclass.joined; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.ParamDef; + +@Entity +@Inheritance(strategy=InheritanceType.JOINED) +@Table(name="ZOOLOGY_ANIMAL") +@FilterDef(name="ignoreSome", parameters={@ParamDef(name="name", type="string")}) +@Filter(name="ignoreSome", condition=":name <> ANIMAL_NAME") +public class Animal { + @Id + @GeneratedValue + @Column(name="ANIMAL_ID") + private Integer id; + + @Column(name="ANIMAL_NAME") + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Club.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Club.java new file mode 100644 index 0000000000..75a04d75e2 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Club.java @@ -0,0 +1,62 @@ +package org.hibernate.test.annotations.filter.subclass.joined; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.FilterDefs; +import org.hibernate.annotations.Filters; +import org.hibernate.annotations.ParamDef; + +@Entity +@FilterDefs({ + @FilterDef(name="iqMin", parameters={@ParamDef(name="min", type="integer")}), + @FilterDef(name="pregnantMembers")}) +public class Club { + @Id + @GeneratedValue + @Column(name="CLUB_ID") + private int id; + + private String name; + + @OneToMany(mappedBy="club") + @Filters({ + @Filter(name="iqMin", table="ZOOLOGY_HUMAN", condition="HUMAN_IQ >= :min"), + @Filter(name="pregnantMembers", table="ZOOLOGY_MAMMAL", condition="IS_PREGNANT=1") + }) + private Set members = new HashSet(); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Set getMembers() { + return members; + } + + public void setMembers(Set members) { + this.members = members; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Human.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Human.java new file mode 100644 index 0000000000..395127e9cf --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Human.java @@ -0,0 +1,43 @@ +package org.hibernate.test.annotations.filter.subclass.joined; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.ParamDef; + +@Entity +@Table(name="ZOOLOGY_HUMAN") +@FilterDef(name="iqRange", parameters= +{ + @ParamDef(name="min", type="integer"), + @ParamDef(name="max", type="integer"), +}) +@Filter(name="iqRange", condition="HUMAN_IQ BETWEEN :min AND :max") +public class Human extends Mammal { + @Column(name="HUMAN_IQ") + private int iq; + + @ManyToOne + private Club club; + + public int getIq() { + return iq; + } + + public void setIq(int iq) { + this.iq = iq; + } + + public Club getClub() { + return club; + } + + public void setClub(Club club) { + this.club = club; + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/JoinedSubClassTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/JoinedSubClassTest.java new file mode 100644 index 0000000000..a03b96f1da --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/JoinedSubClassTest.java @@ -0,0 +1,127 @@ +package org.hibernate.test.annotations.filter.subclass.joined; + +import junit.framework.Assert; + +import org.hibernate.cfg.Configuration; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class JoinedSubClassTest extends BaseCoreFunctionalTestCase{ + + + + @Override + protected void afterConfigurationBuilt(Configuration configuration) { + configuration.setProperty("hibernate.show_sql", "true"); + super.afterConfigurationBuilt(configuration); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{Animal.class, Mammal.class, Human.class, Club.class}; + } + + @Override + protected void prepareTest() throws Exception { + openSession(); + session.beginTransaction(); + + Club club = new Club(); + club.setName("Mensa applicants"); + club.getMembers().add(createHuman(club, false, 90)); + club.getMembers().add(createHuman(club, false, 100)); + club.getMembers().add(createHuman(club, true, 110)); + session.persist(club); + + session.getTransaction().commit(); + session.close(); + } + + @Override + protected void cleanupTest() throws Exception { + openSession(); + session.beginTransaction(); + + session.createQuery("delete from Human").executeUpdate(); + session.createQuery("delete from Club").executeUpdate(); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testIqFilter(){ + openSession(); + session.beginTransaction(); + + assertCount(3); + session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140); + assertCount(1); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testPregnantFilter(){ + openSession(); + session.beginTransaction(); + + assertCount(3); + session.enableFilter("pregnantOnly"); + assertCount(1); + + session.getTransaction().commit(); + session.close(); + } + @Test + public void testNonHumanFilter(){ + openSession(); + session.beginTransaction(); + + assertCount(3); + session.enableFilter("ignoreSome").setParameter("name", "Homo Sapiens"); + assertCount(0); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testClub(){ + openSession(); + session.beginTransaction(); + + Club club = (Club) session.createQuery("from Club").uniqueResult(); + Assert.assertEquals(3, club.getMembers().size()); + session.clear(); + + session.enableFilter("pregnantMembers"); + club = (Club) session.createQuery("from Club").uniqueResult(); + Assert.assertEquals(1, club.getMembers().size()); + session.clear(); + + session.enableFilter("iqMin").setParameter("min", 148); + club = (Club) session.createQuery("from Club").uniqueResult(); + Assert.assertEquals(0, club.getMembers().size()); + + session.getTransaction().commit(); + session.close(); + } + + private Human createHuman(Club club, boolean pregnant, int iq){ + Human human = new Human(); + human.setClub(club); + human.setName("Homo Sapiens"); + human.setPregnant(pregnant); + human.setIq(iq); + session.persist(human); + return human; + } + + private void assertCount(long expected){ + long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult(); + Assert.assertEquals(expected, count); + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Mammal.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Mammal.java new file mode 100644 index 0000000000..0db20e43d6 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/joined/Mammal.java @@ -0,0 +1,26 @@ +package org.hibernate.test.annotations.filter.subclass.joined; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; + +@Entity +@Table(name="ZOOLOGY_MAMMAL") +@FilterDef(name="pregnantOnly") +@Filter(name="pregnantOnly", condition="IS_PREGNANT = 1") +public class Mammal extends Animal{ + + @Column(name="IS_PREGNANT") + private boolean isPregnant; + + public boolean isPregnant() { + return isPregnant; + } + + public void setPregnant(boolean isPregnant) { + this.isPregnant = isPregnant; + } +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Animal.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Animal.java new file mode 100644 index 0000000000..c70f4d0219 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Animal.java @@ -0,0 +1,45 @@ +package org.hibernate.test.annotations.filter.subclass.singletable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.ParamDef; + +@Entity +@Inheritance(strategy=InheritanceType.SINGLE_TABLE) +@Table(name="ZOOLOGY_ANIMAL") +@FilterDef(name="ignoreSome", parameters={@ParamDef(name="name", type="string")}) +@Filter(name="ignoreSome", condition=":name <> ANIMAL_NAME") +public class Animal { + @Id + @GeneratedValue + @Column(name="ANIMAL_ID") + private Integer id; + + @Column(name="ANIMAL_NAME") + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Human.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Human.java new file mode 100644 index 0000000000..f31233b4b2 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Human.java @@ -0,0 +1,31 @@ +package org.hibernate.test.annotations.filter.subclass.singletable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.ParamDef; + +@Entity +@Table(name="ZOOLOGY_HUMAN") +@FilterDef(name="iqRange", parameters= +{ + @ParamDef(name="min", type="integer"), + @ParamDef(name="max", type="integer"), +}) +@Filter(name="iqRange", condition="HUMAN_IQ BETWEEN :min AND :max") +public class Human extends Mammal { + @Column(name="HUMAN_IQ") + private int iq; + + public int getIq() { + return iq; + } + + public void setIq(int iq) { + this.iq = iq; + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Mammal.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Mammal.java new file mode 100644 index 0000000000..47f0f72310 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/Mammal.java @@ -0,0 +1,26 @@ +package org.hibernate.test.annotations.filter.subclass.singletable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; + +@Entity +@Table(name="ZOOLOGY_MAMMAL") +@FilterDef(name="pregnantOnly") +@Filter(name="pregnantOnly", condition="IS_PREGNANT = 1") +public class Mammal extends Animal{ + + @Column(name="IS_PREGNANT") + private boolean isPregnant; + + public boolean isPregnant() { + return isPregnant; + } + + public void setPregnant(boolean isPregnant) { + this.isPregnant = isPregnant; + } +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/SingleTableTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/SingleTableTest.java new file mode 100644 index 0000000000..c4b0b6eedd --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/singletable/SingleTableTest.java @@ -0,0 +1,93 @@ +package org.hibernate.test.annotations.filter.subclass.singletable; + +import junit.framework.Assert; + +import org.hibernate.cfg.Configuration; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class SingleTableTest extends BaseCoreFunctionalTestCase{ + + + + @Override + protected void afterConfigurationBuilt(Configuration configuration) { + configuration.setProperty("hibernate.show_sql", "true"); + super.afterConfigurationBuilt(configuration); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{Animal.class, Mammal.class, Human.class}; + } + + @Override + protected void prepareTest() throws Exception { + createHuman(false, 90); + createHuman(false, 100); + createHuman(true, 110); + } + + @Override + protected void cleanupTest() throws Exception { + openSession(); + session.beginTransaction(); + + session.createQuery("delete from Human").executeUpdate(); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testIqFilter(){ + session.beginTransaction(); + + assertCount(3); + session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140); + assertCount(1); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testPregnantFilter(){ + session.beginTransaction(); + + assertCount(3); + session.enableFilter("pregnantOnly"); + assertCount(1); + + session.getTransaction().commit(); + session.close(); + } + @Test + public void testNonHumanFilter(){ + session.beginTransaction(); + + assertCount(3); + session.enableFilter("ignoreSome").setParameter("name", "Homo Sapiens"); + assertCount(0); + + session.getTransaction().commit(); + session.close(); + } + + private void createHuman(boolean pregnant, int iq){ + openSession(); + session.beginTransaction(); + Human human = new Human(); + human.setName("Homo Sapiens"); + human.setPregnant(pregnant); + human.setIq(iq); + session.persist(human); + session.getTransaction().commit(); + } + + private void assertCount(long expected){ + long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult(); + Assert.assertEquals(expected, count); + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Animal.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Animal.java new file mode 100644 index 0000000000..d4ba534a8c --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Animal.java @@ -0,0 +1,45 @@ +package org.hibernate.test.annotations.filter.subclass.tableperclass; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.ParamDef; + +@Entity +@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) +@Table(name="ZOOLOGY_ANIMAL") +@FilterDef(name="ignoreSome", parameters={@ParamDef(name="name", type="string")}) +@Filter(name="ignoreSome", condition=":name <> ANIMAL_NAME") +public class Animal { + @Id + @GeneratedValue + @Column(name="ANIMAL_ID") + private Integer id; + + @Column(name="ANIMAL_NAME") + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Human.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Human.java new file mode 100644 index 0000000000..b82538f13d --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Human.java @@ -0,0 +1,31 @@ +package org.hibernate.test.annotations.filter.subclass.tableperclass; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.annotations.ParamDef; + +@Entity +@Table(name="ZOOLOGY_HUMAN") +@FilterDef(name="iqRange", parameters= +{ + @ParamDef(name="min", type="integer"), + @ParamDef(name="max", type="integer"), +}) +@Filter(name="iqRange", condition="HUMAN_IQ BETWEEN :min AND :max") +public class Human extends Mammal { + @Column(name="HUMAN_IQ") + private int iq; + + public int getIq() { + return iq; + } + + public void setIq(int iq) { + this.iq = iq; + } + +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Mammal.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Mammal.java new file mode 100644 index 0000000000..7e4af107fa --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/Mammal.java @@ -0,0 +1,26 @@ +package org.hibernate.test.annotations.filter.subclass.tableperclass; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; + +@Entity +@Table(name="ZOOLOGY_MAMMAL") +@FilterDef(name="pregnantOnly") +@Filter(name="pregnantOnly", condition="IS_PREGNANT = 1") +public class Mammal extends Animal{ + + @Column(name="IS_PREGNANT") + private boolean isPregnant; + + public boolean isPregnant() { + return isPregnant; + } + + public void setPregnant(boolean isPregnant) { + this.isPregnant = isPregnant; + } +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/TablePerClassTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/TablePerClassTest.java new file mode 100644 index 0000000000..9e083e38c7 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/filter/subclass/tableperclass/TablePerClassTest.java @@ -0,0 +1,93 @@ +package org.hibernate.test.annotations.filter.subclass.tableperclass; + +import junit.framework.Assert; + +import org.hibernate.cfg.Configuration; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class TablePerClassTest extends BaseCoreFunctionalTestCase{ + + + + @Override + protected void afterConfigurationBuilt(Configuration configuration) { + configuration.setProperty("hibernate.show_sql", "true"); + super.afterConfigurationBuilt(configuration); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{Animal.class, Mammal.class, Human.class}; + } + + @Override + protected void prepareTest() throws Exception { + createHuman(false, 90); + createHuman(false, 100); + createHuman(true, 110); + } + + @Override + protected void cleanupTest() throws Exception { + openSession(); + session.beginTransaction(); + + session.createQuery("delete from Human").executeUpdate(); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testIqFilter(){ + session.beginTransaction(); + + assertCount(3); + session.enableFilter("iqRange").setParameter("min", 101).setParameter("max", 140); + assertCount(1); + + session.getTransaction().commit(); + session.close(); + } + + @Test + public void testPregnantFilter(){ + session.beginTransaction(); + + assertCount(3); + session.enableFilter("pregnantOnly"); + assertCount(1); + + session.getTransaction().commit(); + session.close(); + } + @Test + public void testNonHumanFilter(){ + session.beginTransaction(); + + assertCount(3); + session.enableFilter("ignoreSome").setParameter("name", "Homo Sapiens"); + assertCount(0); + + session.getTransaction().commit(); + session.close(); + } + + private void createHuman(boolean pregnant, int iq){ + openSession(); + session.beginTransaction(); + Human human = new Human(); + human.setName("Homo Sapiens"); + human.setPregnant(pregnant); + human.setIq(iq); + session.persist(human); + session.getTransaction().commit(); + } + + private void assertCount(long expected){ + long count = (Long) session.createQuery("select count(h) from Human h").uniqueResult(); + Assert.assertEquals(expected, count); + } + +}