From 69fda3d61090f26dcba7f1d36cf66734d0b95d62 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Mon, 18 Feb 2013 16:04:24 -0800 Subject: [PATCH] HHH-7436: Reorganized many-to-many annotations tests --- .../test/annotations/manytomany/CatPk.java | 52 -- .../test/annotations/manytomany/City.java | 34 - .../annotations/manytomany/ContactInfo.java | 23 - .../annotations/manytomany/Contractor.java | 24 - .../annotations/manytomany/Inspector.java | 36 - .../test/annotations/manytomany/JobInfo.java | 28 - .../annotations/manytomany/KnownClient.java | 44 - .../manytomany/ManyToManyTest.java | 790 ------------------ .../annotations/manytomany/Permission.java | 31 - .../annotations/manytomany/PhoneNumber.java | 30 - .../manytomany/ProgramManager.java | 32 - .../test/annotations/manytomany/Supplier.java | 45 - .../test/annotations/manytomany/WomanPk.java | 51 -- .../test/annotations/manytomany/Zone.java | 28 - .../{ => associationtable}/Group.java | 26 +- .../{ => associationtable}/GroupWithSet.java | 26 +- .../ManyToManyAssociationTableTest.java | 152 ++++ .../associationtable/Permission.java | 53 ++ .../{ => colrefsuper}/Building.java | 2 +- .../{ => colrefsuper}/BuildingCompany.java | 2 +- .../manytomany/{ => colrefsuper}/Company.java | 2 +- ...oManyReferencedColumnToSuperclassTest.java | 68 ++ .../manytomany/complex/ContactInfo.java | 46 + .../manytomany/complex/Contractor.java | 46 + .../manytomany/{ => complex}/Employee.java | 26 +- .../manytomany/{ => complex}/Employer.java | 30 +- .../manytomany/complex/JobInfo.java | 51 ++ .../complex/ManyToManyComplexTest.java | 340 ++++++++ .../ManyToManyMaxFetchDepth0Test.java | 4 +- .../manytomany/complex/PhoneNumber.java | 53 ++ .../manytomany/complex/ProgramManager.java | 55 ++ .../manytomany/{ => compositepk}/Cat.java | 26 +- .../manytomany/compositepk/CatPk.java | 74 ++ .../manytomany/{ => compositepk}/Man.java | 26 +- .../manytomany/{ => compositepk}/ManPk.java | 26 +- .../ManyToManyCompositePkTest.java | 172 ++++ .../manytomany/{ => compositepk}/Woman.java | 26 +- .../manytomany/compositepk/WomanPk.java | 73 ++ .../manytomany/joinedsubclass/Inspector.java | 58 ++ .../InspectorPrefixes.java | 3 +- .../ManyToManyJoinedSubclassTest.java | 97 +++ .../manytomany/joinedsubclass/Zone.java | 50 ++ .../manytomany/{ => self}/Friend.java | 26 +- .../self/ManyToManySelfReferenceTest.java | 82 ++ .../annotations/manytomany/simple/City.java | 56 ++ .../manytomany/simple/KnownClient.java | 67 ++ .../simple/ManyToManySimpleTest.java | 176 ++++ .../manytomany/{ => simple}/Store.java | 26 +- .../manytomany/simple/Supplier.java | 67 ++ .../manytomany/targetentity/Employee.java | 80 ++ .../manytomany/targetentity/Employer.java | 79 ++ .../ManyToManyTargetEntityTest.java | 145 ++++ 52 files changed, 2388 insertions(+), 1277 deletions(-) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/CatPk.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/City.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Contractor.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Inspector.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/KnownClient.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Permission.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Supplier.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/WomanPk.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Zone.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => associationtable}/Group.java (51%) rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => associationtable}/GroupWithSet.java (50%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/ManyToManyAssociationTableTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Permission.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => colrefsuper}/Building.java (96%) rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => colrefsuper}/BuildingCompany.java (95%) rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => colrefsuper}/Company.java (95%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/ManyToManyReferencedColumnToSuperclassTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ContactInfo.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Contractor.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => complex}/Employee.java (61%) rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => complex}/Employer.java (60%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/JobInfo.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyComplexTest.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => complex}/ManyToManyMaxFetchDepth0Test.java (91%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/PhoneNumber.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ProgramManager.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => compositepk}/Cat.java (51%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/CatPk.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => compositepk}/Man.java (51%) rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => compositepk}/ManPk.java (51%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManyToManyCompositePkTest.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => compositepk}/Woman.java (64%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/WomanPk.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Inspector.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => joinedsubclass}/InspectorPrefixes.java (95%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/ManyToManyJoinedSubclassTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Zone.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => self}/Friend.java (50%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/ManyToManySelfReferenceTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/City.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/KnownClient.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/ManyToManySimpleTest.java rename hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/{ => simple}/Store.java (57%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Supplier.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employee.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employer.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/ManyToManyTargetEntityTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/CatPk.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/CatPk.java deleted file mode 100644 index bec820b176..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/CatPk.java +++ /dev/null @@ -1,52 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import java.io.Serializable; - -import javax.persistence.Column; -import javax.persistence.Embeddable; - -/** - * @author Emmanuel Bernard - */ -@Embeddable -public class CatPk implements Serializable { - private String name; - private String thoroughbred; - - @Column(length=128) - public String getThoroughbred() { - return thoroughbred; - } - - public void setThoroughbred(String thoroughbred) { - this.thoroughbred = thoroughbred; - } - - @Column(length=128) - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public boolean equals(Object o) { - if ( this == o ) return true; - if ( !( o instanceof CatPk ) ) return false; - - final CatPk catPk = (CatPk) o; - - if ( !name.equals( catPk.name ) ) return false; - if ( !thoroughbred.equals( catPk.thoroughbred ) ) return false; - - return true; - } - - public int hashCode() { - int result; - result = name.hashCode(); - result = 29 * result + thoroughbred.hashCode(); - return result; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/City.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/City.java deleted file mode 100644 index 2c91dbbba3..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/City.java +++ /dev/null @@ -1,34 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author Emmanuel Bernard - */ -@Entity -@Table(name = "tbl_city") -public class City { - private Integer id; - private String name; - - @Id - @GeneratedValue - 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/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java deleted file mode 100644 index 6f348033c9..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.hibernate.test.annotations.manytomany; -import java.util.List; -import javax.persistence.CascadeType; -import javax.persistence.Embeddable; -import javax.persistence.ManyToMany; - -@Embeddable -public class ContactInfo { -// @ManyToOne -// Address address; // Unidirectional - - List phoneNumbers; // Bidirectional - - @ManyToMany(cascade= CascadeType.ALL) - public List getPhoneNumbers() { - return phoneNumbers; - } - - public void setPhoneNumbers(List phoneNumbers) { - this.phoneNumbers = phoneNumbers; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Contractor.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Contractor.java deleted file mode 100644 index d1247304a8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Contractor.java +++ /dev/null @@ -1,24 +0,0 @@ -//$Id: Employee.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $ -package org.hibernate.test.annotations.manytomany; -import java.io.Serializable; -import javax.persistence.Entity; - -/** - * Employee in an Employer-Employee relationship - * - * @author Emmanuel Bernard - */ -@Entity -@SuppressWarnings("serial") -public class Contractor extends Employee implements Serializable { - - private float hourlyRate; - - public float getHourlyRate() { - return hourlyRate; - } - - public void setHourlyRate(float hourlyRate) { - this.hourlyRate = hourlyRate; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Inspector.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Inspector.java deleted file mode 100644 index 3dd464d955..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Inspector.java +++ /dev/null @@ -1,36 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import java.io.Serializable; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; - -/** - * @author Emmanuel Bernard - */ -@Entity -@Inheritance(strategy = InheritanceType.JOINED) -class Inspector implements Serializable { - @Id - @GeneratedValue - @Column(name = "id") - private Long _id; - - private String name; - - public Long getId() { - return _id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java deleted file mode 100644 index 6c76fca3eb..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.hibernate.test.annotations.manytomany; -import javax.persistence.CascadeType; -import javax.persistence.Embeddable; -import javax.persistence.ManyToOne; - -@Embeddable -public class JobInfo { - String jobDescription; - ProgramManager pm; // Bidirectional - - public String getJobDescription() { - return jobDescription; - } - - public void setJobDescription( String jobDescription ) { - this.jobDescription = jobDescription; - } - - @ManyToOne( cascade= CascadeType.ALL) - public ProgramManager getPm() { - return pm; - } - - public void setPm( ProgramManager pm ) { - this.pm = pm; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/KnownClient.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/KnownClient.java deleted file mode 100644 index 2b83827d81..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/KnownClient.java +++ /dev/null @@ -1,44 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.ManyToMany; - -/** - * @author Emmanuel Bernard - */ -@Entity -public class KnownClient { - private Integer id; - private String name; - private Set stores; - - @Id - @GeneratedValue - 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; - } - - @ManyToMany(mappedBy = "customers") - public Set getStores() { - return stores; - } - - public void setStores(Set stores) { - this.stores = stores; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java deleted file mode 100644 index 9fcda3079f..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java +++ /dev/null @@ -1,790 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2011, 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.test.annotations.manytomany; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.junit.Test; - -import org.hibernate.Hibernate; -import org.hibernate.JDBCException; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.criterion.Restrictions; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * Many to many tests - * - * @author Emmanuel Bernard - */ -@SuppressWarnings("unchecked") -@FailureExpectedWithNewMetamodel -public class ManyToManyTest - extends BaseCoreFunctionalTestCase { - @Test - public void testDefault() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Store fnac = new Store(); - fnac.setName( "Fnac" ); - KnownClient emmanuel = new KnownClient(); - emmanuel.setName( "Emmanuel" ); - emmanuel.setStores( new HashSet() ); - fnac.setCustomers( new HashSet() ); - fnac.getCustomers().add( emmanuel ); - emmanuel.getStores().add( fnac ); - fnac.setImplantedIn( new HashSet() ); - City paris = new City(); - fnac.getImplantedIn().add( paris ); - paris.setName( "Paris" ); - s.persist( fnac ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - Store store; - KnownClient knownClient; - City city; - store = (Store) s.get( Store.class, fnac.getId() ); - assertNotNull( store ); - assertNotNull( store.getCustomers() ); - assertEquals( 1, store.getCustomers().size() ); - knownClient = store.getCustomers().iterator().next(); - assertEquals( emmanuel.getName(), knownClient.getName() ); - assertNotNull( store.getImplantedIn() ); - assertEquals( 1, store.getImplantedIn().size() ); - city = store.getImplantedIn().iterator().next(); - assertEquals( paris.getName(), city.getName() ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - knownClient = (KnownClient) s.get( KnownClient.class, emmanuel.getId() ); - assertNotNull( knownClient ); - assertNotNull( knownClient.getStores() ); - assertEquals( 1, knownClient.getStores().size() ); - store = knownClient.getStores().iterator().next(); - assertEquals( fnac.getName(), store.getName() ); - tx.commit(); - s.close(); - } - - @Test - public void testCanUseCriteriaQuery() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Store fnac = new Store(); - fnac.setName( "Fnac" ); - Supplier emi = new Supplier(); - emi.setName( "Emmanuel" ); - emi.setSuppStores( new HashSet() ); - fnac.setSuppliers( new HashSet() ); - fnac.getSuppliers().add( emi ); - emi.getSuppStores().add( fnac ); - s.persist( fnac ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - List result = s.createCriteria( Supplier.class ).createAlias( "suppStores", "s" ).add( - Restrictions.eq( "s.name", "Fnac" ) ).list(); - assertEquals( 1, result.size() ); - tx.commit(); - s.close(); - } - - @Test - public void testDefaultCompositePk() throws Exception { - Session s; - Transaction tx; - - s = openSession(); - tx = s.beginTransaction(); - CatPk catPk = new CatPk(); - catPk.setName( "Minou" ); - catPk.setThoroughbred( "Persan" ); - Cat cat = new Cat(); - cat.setId( catPk ); - cat.setAge( 32 ); - Woman woman = new Woman(); - WomanPk womanPk = new WomanPk(); - womanPk.setFirstName( "Emma" ); - womanPk.setLastName( "Peel" ); - woman.setId( womanPk ); - woman.setCats( new HashSet() ); - woman.getCats().add( cat ); - cat.setHumanContacts( new HashSet() ); - cat.getHumanContacts().add( woman ); - s.persist( woman ); - s.persist( cat ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - Cat sameCat = (Cat) s.get( Cat.class, cat.getId() ); - assertNotNull( sameCat ); - assertNotNull( sameCat.getHumanContacts() ); - assertEquals( 1, sameCat.getHumanContacts().size() ); - Woman sameWoman = sameCat.getHumanContacts().iterator().next(); - assertEquals( sameWoman.getId().getLastName(), woman.getId().getLastName() ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - sameWoman = (Woman) s.get( Woman.class, woman.getId() ); - assertNotNull( sameWoman ); - assertNotNull( sameWoman.getCats() ); - assertEquals( 1, sameWoman.getCats().size() ); - sameCat = sameWoman.getCats().iterator().next(); - assertEquals( cat.getAge(), sameCat.getAge() ); - tx.commit(); - s.close(); - } - - @Test - public void testMappedBy() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Store fnac = new Store(); - fnac.setName( "Fnac" ); - Supplier emi = new Supplier(); - emi.setName( "Emmanuel" ); - emi.setSuppStores( new HashSet() ); - fnac.setSuppliers( new HashSet() ); - fnac.getSuppliers().add( emi ); - emi.getSuppStores().add( fnac ); - s.persist( fnac ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - Store store; - Supplier supplier; - store = (Store) s.get( Store.class, fnac.getId() ); - assertNotNull( store ); - assertNotNull( store.getSuppliers() ); - assertEquals( 1, store.getSuppliers().size() ); - supplier = store.getSuppliers().iterator().next(); - assertEquals( emi.getName(), supplier.getName() ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - supplier = (Supplier) s.get( Supplier.class, emi.getId() ); - assertNotNull( supplier ); - assertNotNull( supplier.getSuppStores() ); - assertEquals( 1, supplier.getSuppStores().size() ); - store = supplier.getSuppStores().iterator().next(); - assertEquals( fnac.getName(), store.getName() ); - tx.commit(); - s.close(); - } - - @Test - public void testBasic() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Employer er = new Employer(); - Employee ee = new Employee(); - s.persist( ee ); - Set erColl = new HashSet(); - Collection eeColl = new ArrayList(); - erColl.add( ee ); - eeColl.add( er ); - er.setEmployees( erColl ); - ee.setEmployers( eeColl ); - //s.persist(ee); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - er = (Employer) s.load( Employer.class, er.getId() ); - assertNotNull( er ); - assertNotNull( er.getEmployees() ); - assertEquals( 1, er.getEmployees().size() ); - Employee eeFromDb = (Employee) er.getEmployees().iterator().next(); - assertEquals( ee.getId(), eeFromDb.getId() ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - ee = (Employee) s.get( Employee.class, ee.getId() ); - assertNotNull( ee ); - assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); - tx.commit(); - assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - ee = (Employee) s.get( Employee.class, ee.getId() ); - assertNotNull( ee ); - er = ee.getEmployers().iterator().next(); - assertTrue( "second join non lazy", Hibernate.isInitialized( er ) ); - s.delete( er ); - s.delete( ee ); - tx.commit(); - s.close(); - } - - @Test - public void testOrderByEmployee() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Employer employer = new Employer(); - Employee employee1 = new Employee(); - employee1.setName( "Emmanuel" ); - Employee employee2 = new Employee(); - employee2.setName( "Alice" ); - s.persist( employee1 ); - s.persist( employee2 ); - Set erColl = new HashSet(); - Collection eeColl = new ArrayList(); - Collection eeColl2 = new ArrayList(); - erColl.add( employee1 ); - erColl.add( employee2 ); - eeColl.add( employer ); - eeColl2.add( employer ); - employer.setEmployees( erColl ); - employee1.setEmployers( eeColl ); - employee2.setEmployers( eeColl2 ); - - s.flush(); - s.clear(); - - employer = (Employer) s.get( Employer.class, employer.getId() ); - assertNotNull( employer ); - assertNotNull( employer.getEmployees() ); - assertEquals( 2, employer.getEmployees().size() ); - Employee eeFromDb = (Employee) employer.getEmployees().iterator().next(); - assertEquals( employee2.getName(), eeFromDb.getName() ); - tx.rollback(); - s.close(); - } - - // HHH-4394 - @Test - public void testOrderByContractor() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - - // create some test entities - Employer employer = new Employer(); - Contractor contractor1 = new Contractor(); - contractor1.setName( "Emmanuel" ); - contractor1.setHourlyRate(100.0f); - Contractor contractor2 = new Contractor(); - contractor2.setName( "Hardy" ); - contractor2.setHourlyRate(99.99f); - s.persist( contractor1 ); - s.persist( contractor2 ); - - // add contractors to employer - List setOfContractors = new ArrayList(); - setOfContractors.add( contractor1 ); - setOfContractors.add( contractor2 ); - employer.setContractors( setOfContractors ); - - // add employer to contractors - Collection employerListContractor1 = new ArrayList(); - employerListContractor1.add( employer ); - contractor1.setEmployers( employerListContractor1 ); - - Collection employerListContractor2 = new ArrayList(); - employerListContractor2.add( employer ); - contractor2.setEmployers( employerListContractor2 ); - - s.flush(); - s.clear(); - - // assertions - employer = (Employer) s.get( Employer.class, employer.getId() ); - assertNotNull( employer ); - assertNotNull( employer.getContractors() ); - assertEquals( 2, employer.getContractors().size() ); - Contractor firstContractorFromDb = (Contractor) employer.getContractors().iterator().next(); - assertEquals( contractor2.getName(), firstContractorFromDb.getName() ); - tx.rollback(); - s.close(); - } - - @Test - public void testRemoveInBetween() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Employer er = new Employer(); - Employee ee = new Employee(); - Employee ee2 = new Employee(); - s.persist( ee ); - s.persist( ee2 ); - Set erColl = new HashSet(); - Collection eeColl = new ArrayList(); - erColl.add( ee ); - erColl.add( ee2 ); - eeColl.add( er ); - er.setEmployees( erColl ); - ee.setEmployers( eeColl ); - //s.persist(ee); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - er = (Employer) s.load( Employer.class, er.getId() ); - assertNotNull( er ); - assertNotNull( er.getEmployees() ); - assertEquals( 2, er.getEmployees().size() ); - Iterator iterator = er.getEmployees().iterator(); - Employee eeFromDb = (Employee) iterator.next(); - if ( eeFromDb.getId().equals( ee.getId() ) ) { - eeFromDb = (Employee) iterator.next(); - } - assertEquals( ee2.getId(), eeFromDb.getId() ); - er.getEmployees().remove( eeFromDb ); - eeFromDb.getEmployers().remove( er ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - ee = (Employee) s.get( Employee.class, ee.getId() ); - assertNotNull( ee ); - assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); - tx.commit(); - assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - ee = (Employee) s.get( Employee.class, ee.getId() ); - assertNotNull( ee ); - er = ee.getEmployers().iterator().next(); - assertTrue( "second join non lazy", Hibernate.isInitialized( er ) ); - assertEquals( 1, er.getEmployees().size() ); - s.delete( er ); - s.delete( ee ); - tx.commit(); - s.close(); - } - - @Test - public void testSelf() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Friend f = new Friend(); - Friend sndF = new Friend(); - f.setName( "Starsky" ); - sndF.setName( "Hutch" ); - Set frnds = new HashSet(); - frnds.add( sndF ); - f.setFriends( frnds ); - //Starsky is a friend of Hutch but hutch is not - s.persist( f ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - f = (Friend) s.load( Friend.class, f.getId() ); - assertNotNull( f ); - assertNotNull( f.getFriends() ); - assertEquals( 1, f.getFriends().size() ); - Friend fromDb2ndFrnd = f.getFriends().iterator().next(); - assertEquals( fromDb2ndFrnd.getId(), sndF.getId() ); - assertEquals( 0, fromDb2ndFrnd.getFriends().size() ); - tx.commit(); - s.close(); - } - - @Test - public void testCompositePk() throws Exception { - Session s; - Transaction tx; - - ManPk m1pk = new ManPk(); - m1pk.setElder( true ); - m1pk.setFirstName( "Lucky" ); - m1pk.setLastName( "Luke" ); - ManPk m2pk = new ManPk(); - m2pk.setElder( false ); - m2pk.setFirstName( "Joe" ); - m2pk.setLastName( "Dalton" ); - - Man m1 = new Man(); - m1.setId( m1pk ); - m1.setCarName( "Jolly Jumper" ); - Man m2 = new Man(); - m2.setId( m2pk ); - - WomanPk w1pk = new WomanPk(); - w1pk.setFirstName( "Ma" ); - w1pk.setLastName( "Dalton" ); - WomanPk w2pk = new WomanPk(); - w2pk.setFirstName( "Carla" ); - w2pk.setLastName( "Bruni" ); - - Woman w1 = new Woman(); - w1.setId( w1pk ); - Woman w2 = new Woman(); - w2.setId( w2pk ); - - Set womens = new HashSet(); - womens.add( w1 ); - m1.setWomens( womens ); - Set womens2 = new HashSet(); - womens2.add( w1 ); - womens2.add( w2 ); - m2.setWomens( womens2 ); - - Set mens = new HashSet(); - mens.add( m1 ); - mens.add( m2 ); - w1.setMens( mens ); - Set mens2 = new HashSet(); - mens2.add( m2 ); - w2.setMens( mens2 ); - - s = openSession(); - tx = s.beginTransaction(); - s.persist( m1 ); - s.persist( m2 ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - m1 = (Man) s.load( Man.class, m1pk ); - assertFalse( m1.getWomens().isEmpty() ); - assertEquals( 1, m1.getWomens().size() ); - w1 = (Woman) s.load( Woman.class, w1pk ); - assertFalse( w1.getMens().isEmpty() ); - assertEquals( 2, w1.getMens().size() ); - - tx.commit(); - s.close(); - } - - @Test - public void testAssociationTableUniqueConstraints() throws Exception { - Session s = openSession(); - Permission readAccess = new Permission(); - readAccess.setPermission( "read" ); - readAccess.setExpirationDate( new Date() ); - Collection coll = new ArrayList( 2 ); - coll.add( readAccess ); - coll.add( readAccess ); - Group group = new Group(); - group.setId( new Integer( 1 ) ); - group.setPermissions( coll ); - s.getTransaction().begin(); - try { - s.persist( group ); - s.getTransaction().commit(); - fail( "Unique constraints not applied on association table" ); - } - catch (JDBCException e) { - //success - s.getTransaction().rollback(); - } - finally { - s.close(); - } - } - - @Test - public void testAssociationTableAndOrderBy() throws Exception { - Session s = openSession(); - s.enableFilter( "Groupfilter" ); - Permission readAccess = new Permission(); - readAccess.setPermission( "read" ); - readAccess.setExpirationDate( new Date() ); - Permission writeAccess = new Permission(); - writeAccess.setPermission( "write" ); - writeAccess.setExpirationDate( new Date( new Date().getTime() - 10*60*1000 ) ); - Collection coll = new ArrayList( 2 ); - coll.add( readAccess ); - coll.add( writeAccess ); - Group group = new Group(); - group.setId( new Integer( 1 ) ); - group.setPermissions( coll ); - s.getTransaction().begin(); - s.persist( group ); - s.flush(); - s.clear(); - group = (Group) s.get( Group.class, group.getId() ); - s.createQuery( "select g from Group g join fetch g.permissions").list(); - assertEquals( "write", group.getPermissions().iterator().next().getPermission() ); - s.getTransaction().rollback(); - s.close(); - } - - @Test - public void testAssociationTableAndOrderByWithSet() throws Exception { - Session s = openSession(); - s.enableFilter( "Groupfilter" ); - - Permission readAccess = new Permission(); - readAccess.setPermission( "read" ); - readAccess.setExpirationDate( new Date() ); - - Permission writeAccess = new Permission(); - writeAccess.setPermission( "write" ); - writeAccess.setExpirationDate( new Date( new Date().getTime() - 10*60*1000 ) ); - - Permission executeAccess = new Permission(); - executeAccess.setPermission( "execute" ); - executeAccess.setExpirationDate( new Date( new Date().getTime() - 5*60*1000 ) ); - - Set coll = new HashSet( 3 ); - coll.add( readAccess ); - coll.add( writeAccess ); - coll.add( executeAccess ); - - GroupWithSet group = new GroupWithSet(); - group.setId( new Integer( 1 ) ); - group.setPermissions( coll ); - s.getTransaction().begin(); - s.persist( group ); - s.flush(); - s.clear(); - - group = (GroupWithSet) s.get( GroupWithSet.class, group.getId() ); - s.createQuery( "select g from Group g join fetch g.permissions").list(); - Iterator permIter = group.getPermissions().iterator(); - assertEquals( "write", permIter.next().getPermission() ); - assertEquals( "execute", permIter.next().getPermission() ); - assertEquals( "read", permIter.next().getPermission() ); - s.getTransaction().rollback(); - s.close(); - } - - @Test - public void testJoinedSubclassManyToMany() throws Exception { - Session s = openSession(); - Zone a = new Zone(); - InspectorPrefixes ip = new InspectorPrefixes( "dgi" ); - Transaction tx = s.beginTransaction(); - s.save( a ); - s.save( ip ); - ip.getAreas().add( a ); - tx.commit(); - s.close(); - s = openSession(); - tx = s.beginTransaction(); - ip = (InspectorPrefixes) s.get( InspectorPrefixes.class, ip.getId() ); - assertNotNull( ip ); - assertEquals( 1, ip.getAreas().size() ); - assertEquals( a.getId(), ip.getAreas().get( 0 ).getId() ); - s.delete( ip ); - s.delete( ip.getAreas().get( 0 ) ); - tx.commit(); - s.close(); - } - - @Test - public void testJoinedSubclassManyToManyWithNonPkReference() throws Exception { - Session s = openSession(); - Zone a = new Zone(); - InspectorPrefixes ip = new InspectorPrefixes( "dgi" ); - ip.setName( "Inspector" ); - Transaction tx = s.beginTransaction(); - s.save( a ); - s.save( ip ); - ip.getDesertedAreas().add( a ); - tx.commit(); - s.close(); - s = openSession(); - tx = s.beginTransaction(); - ip = (InspectorPrefixes) s.get( InspectorPrefixes.class, ip.getId() ); - assertNotNull( ip ); - assertEquals( 1, ip.getDesertedAreas().size() ); - assertEquals( a.getId(), ip.getDesertedAreas().get( 0 ).getId() ); - s.delete( ip ); - s.delete( ip.getDesertedAreas().get( 0 ) ); - tx.commit(); - s.close(); - } - - @Test - public void testReferencedColumnNameToSuperclass() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - BuildingCompany comp = new BuildingCompany(); - comp.setFoundedIn( new Date() ); - comp.setName( "Builder century corp."); - s.persist( comp ); - Building building = new Building(); - building.setCompany( comp ); - s.persist( building ); - s.flush(); - s.clear(); - building = (Building) s.get( Building.class, building.getId() ); - assertEquals( comp.getName(), building.getCompany().getName() ); - tx.rollback(); - s.close(); - } - - @Test - @TestForIssue( jiraKey = "HHH-4685" ) - public void testManyToManyEmbeddableBiDirectionalDotNotationInMappedBy() throws Exception { - // Section 11.1.25 - // The ManyToMany annotation may be used within an embeddable class contained within an entity class to specify a - // relationship to a collection of entities[101]. If the relationship is bidirectional and the entity containing - // the embeddable class is the owner of the relationship, the non-owning side must use the mappedBy element of the - // ManyToMany annotation to specify the relationship field or property of the embeddable class. The dot (".") - // notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded - // attribute. The value of each identifier used with the dot notation is the name of the respective embedded field - // or property. - Session s; - s = openSession(); - s.getTransaction().begin(); - Employee e = new Employee(); - e.setName( "Sharon" ); - List phoneNumbers = new ArrayList(); - Collection employees = new ArrayList(); - employees.add( e ); - ContactInfo contactInfo = new ContactInfo(); - PhoneNumber number = new PhoneNumber(); - number.setEmployees( employees ); - phoneNumbers.add( number ); - contactInfo.setPhoneNumbers( phoneNumbers ); - e.setContactInfo( contactInfo ); - s.persist( e ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - e = (Employee)s.get( e.getClass(),e.getId() ); - // follow both directions of many to many association - assertEquals("same employee", e.getName(), e.getContactInfo().getPhoneNumbers().get(0).getEmployees().iterator().next().getName()); - s.getTransaction().commit(); - - s.close(); - } - - @Test - @TestForIssue( jiraKey = "HHH-4685" ) - public void testOneToManyEmbeddableBiDirectionalDotNotationInMappedBy() throws Exception { - // Section 11.1.26 - // The ManyToOne annotation may be used within an embeddable class to specify a relationship from the embeddable - // class to an entity class. If the relationship is bidirectional, the non-owning OneToMany entity side must use the - // mappedBy element of the OneToMany annotation to specify the relationship field or property of the embeddable field - // or property on the owning side of the relationship. The dot (".") notation syntax must be used in the mappedBy - // element to indicate the relationship attribute within the embedded attribute. The value of each identifier used - // with the dot notation is the name of the respective embedded field or property. - Session s; - s = openSession(); - s.getTransaction().begin(); - Employee e = new Employee(); - JobInfo job = new JobInfo(); - job.setJobDescription( "Sushi Chef" ); - ProgramManager pm = new ProgramManager(); - Collection employees = new ArrayList(); - employees.add(e); - pm.setManages( employees ); - job.setPm(pm); - e.setJobInfo( job ); - s.persist( e ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - e = (Employee) s.get( e.getClass(), e.getId() ); - assertEquals( "same job in both directions", - e.getJobInfo().getJobDescription(), - e.getJobInfo().getPm().getManages().iterator().next().getJobInfo().getJobDescription() ); - s.getTransaction().commit(); - s.close(); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ - Friend.class, - Employer.class, - Employee.class, - Contractor.class, - Man.class, - Woman.class, - Store.class, - KnownClient.class, - Supplier.class, - City.class, - Cat.class, - Group.class, - GroupWithSet.class, - Permission.class, - Zone.class, - Inspector.class, - InspectorPrefixes.class, - BuildingCompany.class, - Building.class, - PhoneNumber.class, - ProgramManager.class - }; - } - -} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Permission.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Permission.java deleted file mode 100644 index 1eef64bf34..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Permission.java +++ /dev/null @@ -1,31 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import java.util.Date; -import javax.persistence.Entity; -import javax.persistence.Id; - -/** - * @author Emmanuel Bernard - */ -@Entity -public class Permission { - private String permission; - private Date expirationDate; - - @Id - public String getPermission() { - return permission; - } - - public void setPermission(String permission) { - this.permission = permission; - } - - public Date getExpirationDate() { - return expirationDate; - } - - public void setExpirationDate(Date expirationDate) { - this.expirationDate = expirationDate; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java deleted file mode 100644 index 024d2e10cf..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.hibernate.test.annotations.manytomany; -import java.util.Collection; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToMany; - -@Entity -public class PhoneNumber { - int phNumber; - Collection employees; - - @Id - public int getPhNumber() { - return phNumber; - } - - public void setPhNumber(int phNumber) { - this.phNumber = phNumber; - } - - @ManyToMany(mappedBy="contactInfo.phoneNumbers", cascade= CascadeType.ALL) - public Collection getEmployees() { - return employees; - } - - public void setEmployees(Collection employees) { - this.employees = employees; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java deleted file mode 100644 index 4410178bc0..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.hibernate.test.annotations.manytomany; -import java.util.Collection; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.OneToMany; - -@Entity -public class ProgramManager { - int id; - - Collection manages; - - @Id - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @OneToMany( mappedBy="jobInfo.pm", cascade= CascadeType.ALL ) - public Collection getManages() { - return manages; - } - - public void setManages( Collection manages ) { - this.manages = manages; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Supplier.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Supplier.java deleted file mode 100644 index df4dd3b1ef..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Supplier.java +++ /dev/null @@ -1,45 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.ManyToMany; - -/** - * @author Emmanuel Bernard - */ -@Entity -public class Supplier { - private Integer id; - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - private Set suppStores; - - @Id - @GeneratedValue - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - @ManyToMany(mappedBy = "suppliers") - public Set getSuppStores() { - return suppStores; - } - - public void setSuppStores(Set suppStores) { - this.suppStores = suppStores; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/WomanPk.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/WomanPk.java deleted file mode 100644 index 9263f431e1..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/WomanPk.java +++ /dev/null @@ -1,51 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import java.io.Serializable; -import javax.persistence.Column; -import javax.persistence.Embeddable; - -/** - * @author Emmanuel Bernard - */ -@Embeddable -public class WomanPk implements Serializable { - - - private String firstName; - private String lastName; - - public int hashCode() { - //this implem sucks - return getFirstName().hashCode() + getLastName().hashCode(); - } - - public boolean equals(Object obj) { - //firstName and lastName are expected to be set in this implem - if ( obj != null && obj instanceof WomanPk ) { - WomanPk other = (WomanPk) obj; - return getFirstName().equals( other.getFirstName() ) - && getLastName().equals( other.getLastName() ); - } - else { - return false; - } - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - @Column(length=128) - public String getFirstName() { - return firstName; - } - - @Column(length=128) - public String getLastName() { - return lastName; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Zone.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Zone.java deleted file mode 100644 index 931f1d881f..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Zone.java +++ /dev/null @@ -1,28 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author Emmanuel Bernard - */ -@Entity -@Table(name="`Zone`") -public class Zone { - private Integer id; - - @Id - @GeneratedValue - @Column(name = "id") - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Group.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Group.java similarity index 51% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Group.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Group.java index d7d7f4662a..8fac612e8d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Group.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Group.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.associationtable; import java.util.Collection; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/GroupWithSet.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/GroupWithSet.java similarity index 50% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/GroupWithSet.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/GroupWithSet.java index 01bbe3b91a..da8a71cd1e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/GroupWithSet.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/GroupWithSet.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.associationtable; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/ManyToManyAssociationTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/ManyToManyAssociationTableTest.java new file mode 100644 index 0000000000..03880e5fcf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/ManyToManyAssociationTableTest.java @@ -0,0 +1,152 @@ +/* + * 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.test.annotations.manytomany.associationtable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.JDBCException; +import org.hibernate.Session; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +@FailureExpectedWithNewMetamodel +public class ManyToManyAssociationTableTest extends BaseCoreFunctionalTestCase { + + @Test + public void testAssociationTableUniqueConstraints() throws Exception { + Session s = openSession(); + Permission readAccess = new Permission(); + readAccess.setPermission( "read" ); + readAccess.setExpirationDate( new Date() ); + Collection coll = new ArrayList( 2 ); + coll.add( readAccess ); + coll.add( readAccess ); + Group group = new Group(); + group.setId( new Integer( 1 ) ); + group.setPermissions( coll ); + s.getTransaction().begin(); + try { + s.persist( group ); + s.getTransaction().commit(); + fail( "Unique constraints not applied on association table" ); + } + catch (JDBCException e) { + //success + s.getTransaction().rollback(); + } + finally { + s.close(); + } + } + + @Test + public void testAssociationTableAndOrderBy() throws Exception { + Session s = openSession(); + s.enableFilter( "Groupfilter" ); + Permission readAccess = new Permission(); + readAccess.setPermission( "read" ); + readAccess.setExpirationDate( new Date() ); + Permission writeAccess = new Permission(); + writeAccess.setPermission( "write" ); + writeAccess.setExpirationDate( new Date( new Date().getTime() - 10*60*1000 ) ); + Collection coll = new ArrayList( 2 ); + coll.add( readAccess ); + coll.add( writeAccess ); + Group group = new Group(); + group.setId( new Integer( 1 ) ); + group.setPermissions( coll ); + s.getTransaction().begin(); + s.persist( group ); + s.flush(); + s.clear(); + group = (Group) s.get( Group.class, group.getId() ); + s.createQuery( "select g from Group g join fetch g.permissions").list(); + assertEquals( "write", group.getPermissions().iterator().next().getPermission() ); + s.getTransaction().rollback(); + s.close(); + } + + @Test + public void testAssociationTableAndOrderByWithSet() throws Exception { + Session s = openSession(); + s.enableFilter( "Groupfilter" ); + + Permission readAccess = new Permission(); + readAccess.setPermission( "read" ); + readAccess.setExpirationDate( new Date() ); + + Permission writeAccess = new Permission(); + writeAccess.setPermission( "write" ); + writeAccess.setExpirationDate( new Date( new Date().getTime() - 10*60*1000 ) ); + + Permission executeAccess = new Permission(); + executeAccess.setPermission( "execute" ); + executeAccess.setExpirationDate( new Date( new Date().getTime() - 5*60*1000 ) ); + + Set coll = new HashSet( 3 ); + coll.add( readAccess ); + coll.add( writeAccess ); + coll.add( executeAccess ); + + GroupWithSet group = new GroupWithSet(); + group.setId( new Integer( 1 ) ); + group.setPermissions( coll ); + s.getTransaction().begin(); + s.persist( group ); + s.flush(); + s.clear(); + + group = (GroupWithSet) s.get( GroupWithSet.class, group.getId() ); + s.createQuery( "select g from Group g join fetch g.permissions").list(); + Iterator permIter = group.getPermissions().iterator(); + assertEquals( "write", permIter.next().getPermission() ); + assertEquals( "execute", permIter.next().getPermission() ); + assertEquals( "read", permIter.next().getPermission() ); + s.getTransaction().rollback(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Group.class, + GroupWithSet.class, + Permission.class + }; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Permission.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Permission.java new file mode 100644 index 0000000000..e14a2fa961 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/associationtable/Permission.java @@ -0,0 +1,53 @@ +/* + * 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.test.annotations.manytomany.associationtable; +import java.util.Date; +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class Permission { + private String permission; + private Date expirationDate; + + @Id + public String getPermission() { + return permission; + } + + public void setPermission(String permission) { + this.permission = permission; + } + + public Date getExpirationDate() { + return expirationDate; + } + + public void setExpirationDate(Date expirationDate) { + this.expirationDate = expirationDate; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Building.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/Building.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Building.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/Building.java index 1bb2c52798..3aaec80933 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Building.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/Building.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.annotations.manytomany; +package org.hibernate.test.annotations.manytomany.colrefsuper; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/BuildingCompany.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/BuildingCompany.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/BuildingCompany.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/BuildingCompany.java index 3c2b870939..87eec38b67 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/BuildingCompany.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/BuildingCompany.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.annotations.manytomany; +package org.hibernate.test.annotations.manytomany.colrefsuper; import java.util.Date; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Company.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/Company.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Company.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/Company.java index 059791cb01..3c98ac47aa 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Company.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/Company.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.annotations.manytomany; +package org.hibernate.test.annotations.manytomany.colrefsuper; import java.io.Serializable; import javax.persistence.Column; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/ManyToManyReferencedColumnToSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/ManyToManyReferencedColumnToSuperclassTest.java new file mode 100644 index 0000000000..13123dfe0a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/colrefsuper/ManyToManyReferencedColumnToSuperclassTest.java @@ -0,0 +1,68 @@ +/* + * 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.test.annotations.manytomany.colrefsuper; + +import java.util.Date; + +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +public class ManyToManyReferencedColumnToSuperclassTest extends BaseCoreFunctionalTestCase { + + @Test + public void testReferencedColumnNameToSuperclass() throws Exception { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + BuildingCompany comp = new BuildingCompany(); + comp.setFoundedIn( new Date() ); + comp.setName( "Builder century corp."); + s.persist( comp ); + Building building = new Building(); + building.setCompany( comp ); + s.persist( building ); + s.flush(); + s.clear(); + building = (Building) s.get( Building.class, building.getId() ); + assertEquals( comp.getName(), building.getCompany().getName() ); + tx.rollback(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + BuildingCompany.class, + Building.class + }; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ContactInfo.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ContactInfo.java new file mode 100644 index 0000000000..1665f6b51e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ContactInfo.java @@ -0,0 +1,46 @@ +/* + * 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.test.annotations.manytomany.complex; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.Embeddable; +import javax.persistence.ManyToMany; + +@Embeddable +public class ContactInfo { +// @ManyToOne +// Address address; // Unidirectional + + List phoneNumbers; // Bidirectional + + @ManyToMany(cascade= CascadeType.ALL) + public List getPhoneNumbers() { + return phoneNumbers; + } + + public void setPhoneNumbers(List phoneNumbers) { + this.phoneNumbers = phoneNumbers; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Contractor.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Contractor.java new file mode 100644 index 0000000000..b00b1780d6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Contractor.java @@ -0,0 +1,46 @@ +/* + * 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.test.annotations.manytomany.complex; +import java.io.Serializable; +import javax.persistence.Entity; + +/** + * Employee in an Employer-Employee relationship + * + * @author Emmanuel Bernard + */ +@Entity +@SuppressWarnings("serial") +public class Contractor extends Employee implements Serializable { + + private float hourlyRate; + + public float getHourlyRate() { + return hourlyRate; + } + + public void setHourlyRate(float hourlyRate) { + this.hourlyRate = hourlyRate; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Employee.java similarity index 61% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Employee.java index 46791105b9..7d798f227d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Employee.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.complex; import java.io.Serializable; import java.util.Collection; import javax.persistence.CascadeType; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Employer.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Employer.java similarity index 60% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Employer.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Employer.java index 6da0e16208..1abd8f4f52 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Employer.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/Employer.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.complex; import java.io.Serializable; import java.util.Collection; import java.util.List; @@ -29,7 +51,7 @@ public class Employer implements Serializable { private List contractors; @ManyToMany( - targetEntity = org.hibernate.test.annotations.manytomany.Contractor.class, + targetEntity = Contractor.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE} ) @JoinTable( @@ -48,7 +70,7 @@ public class Employer implements Serializable { } @ManyToMany( - targetEntity = org.hibernate.test.annotations.manytomany.Employee.class, + targetEntity = Employee.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE} ) @JoinTable( diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/JobInfo.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/JobInfo.java new file mode 100644 index 0000000000..5f071dff78 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/JobInfo.java @@ -0,0 +1,51 @@ +/* + * 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.test.annotations.manytomany.complex; +import javax.persistence.CascadeType; +import javax.persistence.Embeddable; +import javax.persistence.ManyToOne; + +@Embeddable +public class JobInfo { + String jobDescription; + ProgramManager pm; // Bidirectional + + public String getJobDescription() { + return jobDescription; + } + + public void setJobDescription( String jobDescription ) { + this.jobDescription = jobDescription; + } + + @ManyToOne( cascade= CascadeType.ALL) + public ProgramManager getPm() { + return pm; + } + + public void setPm( ProgramManager pm ) { + this.pm = pm; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyComplexTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyComplexTest.java new file mode 100644 index 0000000000..165ef522df --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyComplexTest.java @@ -0,0 +1,340 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.test.annotations.manytomany.complex; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Many to many tests + * + * @author Emmanuel Bernard + */ +@SuppressWarnings("unchecked") +@FailureExpectedWithNewMetamodel +public class ManyToManyComplexTest + extends BaseCoreFunctionalTestCase { + + @Test + public void testBasic() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Employer er = new Employer(); + Employee ee = new Employee(); + s.persist( ee ); + Set erColl = new HashSet(); + Collection eeColl = new ArrayList(); + erColl.add( ee ); + eeColl.add( er ); + er.setEmployees( erColl ); + ee.setEmployers( eeColl ); + //s.persist(ee); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + er = (Employer) s.load( Employer.class, er.getId() ); + assertNotNull( er ); + assertNotNull( er.getEmployees() ); + assertEquals( 1, er.getEmployees().size() ); + Employee eeFromDb = (Employee) er.getEmployees().iterator().next(); + assertEquals( ee.getId(), eeFromDb.getId() ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + ee = (Employee) s.get( Employee.class, ee.getId() ); + assertNotNull( ee ); + assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); + tx.commit(); + assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + ee = (Employee) s.get( Employee.class, ee.getId() ); + assertNotNull( ee ); + er = ee.getEmployers().iterator().next(); + assertTrue( "second join non lazy", Hibernate.isInitialized( er ) ); + s.delete( er ); + s.delete( ee ); + tx.commit(); + s.close(); + } + + @Test + public void testOrderByEmployee() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Employer employer = new Employer(); + Employee employee1 = new Employee(); + employee1.setName( "Emmanuel" ); + Employee employee2 = new Employee(); + employee2.setName( "Alice" ); + s.persist( employee1 ); + s.persist( employee2 ); + Set erColl = new HashSet(); + Collection eeColl = new ArrayList(); + Collection eeColl2 = new ArrayList(); + erColl.add( employee1 ); + erColl.add( employee2 ); + eeColl.add( employer ); + eeColl2.add( employer ); + employer.setEmployees( erColl ); + employee1.setEmployers( eeColl ); + employee2.setEmployers( eeColl2 ); + + s.flush(); + s.clear(); + + employer = (Employer) s.get( Employer.class, employer.getId() ); + assertNotNull( employer ); + assertNotNull( employer.getEmployees() ); + assertEquals( 2, employer.getEmployees().size() ); + Employee eeFromDb = (Employee) employer.getEmployees().iterator().next(); + assertEquals( employee2.getName(), eeFromDb.getName() ); + tx.rollback(); + s.close(); + } + + // HHH-4394 + @Test + public void testOrderByContractor() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + + // create some test entities + Employer employer = new Employer(); + Contractor contractor1 = new Contractor(); + contractor1.setName( "Emmanuel" ); + contractor1.setHourlyRate(100.0f); + Contractor contractor2 = new Contractor(); + contractor2.setName( "Hardy" ); + contractor2.setHourlyRate(99.99f); + s.persist( contractor1 ); + s.persist( contractor2 ); + + // add contractors to employer + List setOfContractors = new ArrayList(); + setOfContractors.add( contractor1 ); + setOfContractors.add( contractor2 ); + employer.setContractors( setOfContractors ); + + // add employer to contractors + Collection employerListContractor1 = new ArrayList(); + employerListContractor1.add( employer ); + contractor1.setEmployers( employerListContractor1 ); + + Collection employerListContractor2 = new ArrayList(); + employerListContractor2.add( employer ); + contractor2.setEmployers( employerListContractor2 ); + + s.flush(); + s.clear(); + + // assertions + employer = (Employer) s.get( Employer.class, employer.getId() ); + assertNotNull( employer ); + assertNotNull( employer.getContractors() ); + assertEquals( 2, employer.getContractors().size() ); + Contractor firstContractorFromDb = (Contractor) employer.getContractors().iterator().next(); + assertEquals( contractor2.getName(), firstContractorFromDb.getName() ); + tx.rollback(); + s.close(); + } + + @Test + public void testRemoveInBetween() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Employer er = new Employer(); + Employee ee = new Employee(); + Employee ee2 = new Employee(); + s.persist( ee ); + s.persist( ee2 ); + Set erColl = new HashSet(); + Collection eeColl = new ArrayList(); + erColl.add( ee ); + erColl.add( ee2 ); + eeColl.add( er ); + er.setEmployees( erColl ); + ee.setEmployers( eeColl ); + //s.persist(ee); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + er = (Employer) s.load( Employer.class, er.getId() ); + assertNotNull( er ); + assertNotNull( er.getEmployees() ); + assertEquals( 2, er.getEmployees().size() ); + Iterator iterator = er.getEmployees().iterator(); + Employee eeFromDb = (Employee) iterator.next(); + if ( eeFromDb.getId().equals( ee.getId() ) ) { + eeFromDb = (Employee) iterator.next(); + } + assertEquals( ee2.getId(), eeFromDb.getId() ); + er.getEmployees().remove( eeFromDb ); + eeFromDb.getEmployers().remove( er ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + ee = (Employee) s.get( Employee.class, ee.getId() ); + assertNotNull( ee ); + assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); + tx.commit(); + assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + ee = (Employee) s.get( Employee.class, ee.getId() ); + assertNotNull( ee ); + er = ee.getEmployers().iterator().next(); + assertTrue( "second join non lazy", Hibernate.isInitialized( er ) ); + assertEquals( 1, er.getEmployees().size() ); + s.delete( er ); + s.delete( ee ); + tx.commit(); + s.close(); + } + + @Test + @TestForIssue( jiraKey = "HHH-4685" ) + public void testManyToManyEmbeddableBiDirectionalDotNotationInMappedBy() throws Exception { + // Section 11.1.25 + // The ManyToMany annotation may be used within an embeddable class contained within an entity class to specify a + // relationship to a collection of entities[101]. If the relationship is bidirectional and the entity containing + // the embeddable class is the owner of the relationship, the non-owning side must use the mappedBy element of the + // ManyToMany annotation to specify the relationship field or property of the embeddable class. The dot (".") + // notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded + // attribute. The value of each identifier used with the dot notation is the name of the respective embedded field + // or property. + Session s; + s = openSession(); + s.getTransaction().begin(); + Employee e = new Employee(); + e.setName( "Sharon" ); + List phoneNumbers = new ArrayList(); + Collection employees = new ArrayList(); + employees.add( e ); + ContactInfo contactInfo = new ContactInfo(); + PhoneNumber number = new PhoneNumber(); + number.setEmployees( employees ); + phoneNumbers.add( number ); + contactInfo.setPhoneNumbers( phoneNumbers ); + e.setContactInfo( contactInfo ); + s.persist( e ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + e = (Employee)s.get( e.getClass(),e.getId() ); + // follow both directions of many to many association + assertEquals("same employee", e.getName(), e.getContactInfo().getPhoneNumbers().get(0).getEmployees().iterator().next().getName()); + s.getTransaction().commit(); + + s.close(); + } + + @Test + @TestForIssue( jiraKey = "HHH-4685" ) + public void testOneToManyEmbeddableBiDirectionalDotNotationInMappedBy() throws Exception { + // Section 11.1.26 + // The ManyToOne annotation may be used within an embeddable class to specify a relationship from the embeddable + // class to an entity class. If the relationship is bidirectional, the non-owning OneToMany entity side must use the + // mappedBy element of the OneToMany annotation to specify the relationship field or property of the embeddable field + // or property on the owning side of the relationship. The dot (".") notation syntax must be used in the mappedBy + // element to indicate the relationship attribute within the embedded attribute. The value of each identifier used + // with the dot notation is the name of the respective embedded field or property. + Session s; + s = openSession(); + s.getTransaction().begin(); + Employee e = new Employee(); + JobInfo job = new JobInfo(); + job.setJobDescription( "Sushi Chef" ); + ProgramManager pm = new ProgramManager(); + Collection employees = new ArrayList(); + employees.add(e); + pm.setManages( employees ); + job.setPm(pm); + e.setJobInfo( job ); + s.persist( e ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.getTransaction().begin(); + e = (Employee) s.get( e.getClass(), e.getId() ); + assertEquals( "same job in both directions", + e.getJobInfo().getJobDescription(), + e.getJobInfo().getPm().getManages().iterator().next().getJobInfo().getJobDescription() ); + s.getTransaction().commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Employer.class, + Employee.class, + Contractor.class, + PhoneNumber.class, + ProgramManager.class + }; + } + +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyMaxFetchDepth0Test.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyMaxFetchDepth0Test.java index bb329cb217..af2c582dcc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyMaxFetchDepth0Test.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ManyToManyMaxFetchDepth0Test.java @@ -22,7 +22,7 @@ * Boston, MA 02110-1301 USA * */ -package org.hibernate.test.annotations.manytomany; +package org.hibernate.test.annotations.manytomany.complex; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; @@ -35,7 +35,7 @@ import org.hibernate.testing.FailureExpectedWithNewMetamodel; */ @SuppressWarnings("unchecked") @FailureExpectedWithNewMetamodel -public class ManyToManyMaxFetchDepth0Test extends ManyToManyTest { +public class ManyToManyMaxFetchDepth0Test extends ManyToManyComplexTest { @Override protected void configure(Configuration cfg) { cfg.setProperty( Environment.MAX_FETCH_DEPTH, "0" ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/PhoneNumber.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/PhoneNumber.java new file mode 100644 index 0000000000..d3f1ad83cc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/PhoneNumber.java @@ -0,0 +1,53 @@ +/* + * 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.test.annotations.manytomany.complex; +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +@Entity +public class PhoneNumber { + int phNumber; + Collection employees; + + @Id + public int getPhNumber() { + return phNumber; + } + + public void setPhNumber(int phNumber) { + this.phNumber = phNumber; + } + + @ManyToMany(mappedBy="contactInfo.phoneNumbers", cascade= CascadeType.ALL) + public Collection getEmployees() { + return employees; + } + + public void setEmployees(Collection employees) { + this.employees = employees; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ProgramManager.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ProgramManager.java new file mode 100644 index 0000000000..83f25df039 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/complex/ProgramManager.java @@ -0,0 +1,55 @@ +/* + * 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.test.annotations.manytomany.complex; +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class ProgramManager { + int id; + + Collection manages; + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @OneToMany( mappedBy="jobInfo.pm", cascade= CascadeType.ALL ) + public Collection getManages() { + return manages; + } + + public void setManages( Collection manages ) { + this.manages = manages; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Cat.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Cat.java similarity index 51% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Cat.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Cat.java index 7e5d6d83af..6773eff241 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Cat.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Cat.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.compositepk; import java.util.Set; import javax.persistence.EmbeddedId; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/CatPk.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/CatPk.java new file mode 100644 index 0000000000..c6d0c1ea4c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/CatPk.java @@ -0,0 +1,74 @@ +/* + * 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.test.annotations.manytomany.compositepk; +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +/** + * @author Emmanuel Bernard + */ +@Embeddable +public class CatPk implements Serializable { + private String name; + private String thoroughbred; + + @Column(length=128) + public String getThoroughbred() { + return thoroughbred; + } + + public void setThoroughbred(String thoroughbred) { + this.thoroughbred = thoroughbred; + } + + @Column(length=128) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean equals(Object o) { + if ( this == o ) return true; + if ( !( o instanceof CatPk ) ) return false; + + final CatPk catPk = (CatPk) o; + + if ( !name.equals( catPk.name ) ) return false; + if ( !thoroughbred.equals( catPk.thoroughbred ) ) return false; + + return true; + } + + public int hashCode() { + int result; + result = name.hashCode(); + result = 29 * result + thoroughbred.hashCode(); + return result; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Man.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Man.java similarity index 51% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Man.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Man.java index 6401a5efc8..f6e6237102 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Man.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Man.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.compositepk; import java.io.Serializable; import java.util.Set; import javax.persistence.CascadeType; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManPk.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManPk.java similarity index 51% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManPk.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManPk.java index 9b0cb2f9bc..9cf3d0adca 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/ManPk.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManPk.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.compositepk; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManyToManyCompositePkTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManyToManyCompositePkTest.java new file mode 100644 index 0000000000..9be4e2289d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/ManyToManyCompositePkTest.java @@ -0,0 +1,172 @@ +/* + * 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.test.annotations.manytomany.compositepk; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +@FailureExpectedWithNewMetamodel +public class ManyToManyCompositePkTest extends BaseCoreFunctionalTestCase { + + @Test + public void testDefaultCompositePk() throws Exception { + Session s; + Transaction tx; + + s = openSession(); + tx = s.beginTransaction(); + CatPk catPk = new CatPk(); + catPk.setName( "Minou" ); + catPk.setThoroughbred( "Persan" ); + Cat cat = new Cat(); + cat.setId( catPk ); + cat.setAge( 32 ); + Woman woman = new Woman(); + WomanPk womanPk = new WomanPk(); + womanPk.setFirstName( "Emma" ); + womanPk.setLastName( "Peel" ); + woman.setId( womanPk ); + woman.setCats( new HashSet() ); + woman.getCats().add( cat ); + cat.setHumanContacts( new HashSet() ); + cat.getHumanContacts().add( woman ); + s.persist( woman ); + s.persist( cat ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + Cat sameCat = (Cat) s.get( Cat.class, cat.getId() ); + assertNotNull( sameCat ); + assertNotNull( sameCat.getHumanContacts() ); + assertEquals( 1, sameCat.getHumanContacts().size() ); + Woman sameWoman = sameCat.getHumanContacts().iterator().next(); + assertEquals( sameWoman.getId().getLastName(), woman.getId().getLastName() ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + sameWoman = (Woman) s.get( Woman.class, woman.getId() ); + assertNotNull( sameWoman ); + assertNotNull( sameWoman.getCats() ); + assertEquals( 1, sameWoman.getCats().size() ); + sameCat = sameWoman.getCats().iterator().next(); + assertEquals( cat.getAge(), sameCat.getAge() ); + tx.commit(); + s.close(); + } + + @Test + public void testCompositePk() throws Exception { + Session s; + Transaction tx; + + ManPk m1pk = new ManPk(); + m1pk.setElder( true ); + m1pk.setFirstName( "Lucky" ); + m1pk.setLastName( "Luke" ); + ManPk m2pk = new ManPk(); + m2pk.setElder( false ); + m2pk.setFirstName( "Joe" ); + m2pk.setLastName( "Dalton" ); + + Man m1 = new Man(); + m1.setId( m1pk ); + m1.setCarName( "Jolly Jumper" ); + Man m2 = new Man(); + m2.setId( m2pk ); + + WomanPk w1pk = new WomanPk(); + w1pk.setFirstName( "Ma" ); + w1pk.setLastName( "Dalton" ); + WomanPk w2pk = new WomanPk(); + w2pk.setFirstName( "Carla" ); + w2pk.setLastName( "Bruni" ); + + Woman w1 = new Woman(); + w1.setId( w1pk ); + Woman w2 = new Woman(); + w2.setId( w2pk ); + + Set womens = new HashSet(); + womens.add( w1 ); + m1.setWomens( womens ); + Set womens2 = new HashSet(); + womens2.add( w1 ); + womens2.add( w2 ); + m2.setWomens( womens2 ); + + Set mens = new HashSet(); + mens.add( m1 ); + mens.add( m2 ); + w1.setMens( mens ); + Set mens2 = new HashSet(); + mens2.add( m2 ); + w2.setMens( mens2 ); + + s = openSession(); + tx = s.beginTransaction(); + s.persist( m1 ); + s.persist( m2 ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + m1 = (Man) s.load( Man.class, m1pk ); + assertFalse( m1.getWomens().isEmpty() ); + assertEquals( 1, m1.getWomens().size() ); + w1 = (Woman) s.load( Woman.class, w1pk ); + assertFalse( w1.getMens().isEmpty() ); + assertEquals( 2, w1.getMens().size() ); + + tx.commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Man.class, + Woman.class, + Cat.class, + }; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Woman.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Woman.java similarity index 64% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Woman.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Woman.java index bfb478ed9b..c9172d0326 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Woman.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/Woman.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.compositepk; import java.io.Serializable; import java.util.Set; import javax.persistence.CascadeType; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/WomanPk.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/WomanPk.java new file mode 100644 index 0000000000..85a67a714a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/compositepk/WomanPk.java @@ -0,0 +1,73 @@ +/* + * 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.test.annotations.manytomany.compositepk; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Embeddable; + +/** + * @author Emmanuel Bernard + */ +@Embeddable +public class WomanPk implements Serializable { + + + private String firstName; + private String lastName; + + public int hashCode() { + //this implem sucks + return getFirstName().hashCode() + getLastName().hashCode(); + } + + public boolean equals(Object obj) { + //firstName and lastName are expected to be set in this implem + if ( obj != null && obj instanceof WomanPk ) { + WomanPk other = (WomanPk) obj; + return getFirstName().equals( other.getFirstName() ) + && getLastName().equals( other.getLastName() ); + } + else { + return false; + } + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Column(length=128) + public String getFirstName() { + return firstName; + } + + @Column(length=128) + public String getLastName() { + return lastName; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Inspector.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Inspector.java new file mode 100644 index 0000000000..8f70ff89d5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Inspector.java @@ -0,0 +1,58 @@ +/* + * 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.test.annotations.manytomany.joinedsubclass; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +/** + * @author Emmanuel Bernard + */ +@Entity +@Inheritance(strategy = InheritanceType.JOINED) +class Inspector implements Serializable { + @Id + @GeneratedValue + @Column(name = "id") + private Long _id; + + private String name; + + public Long getId() { + return _id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/InspectorPrefixes.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/InspectorPrefixes.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/InspectorPrefixes.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/InspectorPrefixes.java index 60f8227624..0228042db5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/InspectorPrefixes.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/InspectorPrefixes.java @@ -1,5 +1,4 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +package org.hibernate.test.annotations.manytomany.joinedsubclass; import java.util.ArrayList; import java.util.List; import javax.persistence.Column; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/ManyToManyJoinedSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/ManyToManyJoinedSubclassTest.java new file mode 100644 index 0000000000..2429b1d6ee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/ManyToManyJoinedSubclassTest.java @@ -0,0 +1,97 @@ +/* + * 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.test.annotations.manytomany.joinedsubclass; + +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +@FailureExpectedWithNewMetamodel +public class ManyToManyJoinedSubclassTest extends BaseCoreFunctionalTestCase { + + @Test + public void testJoinedSubclassManyToMany() throws Exception { + Session s = openSession(); + Zone a = new Zone(); + InspectorPrefixes ip = new InspectorPrefixes( "dgi" ); + Transaction tx = s.beginTransaction(); + s.save( a ); + s.save( ip ); + ip.getAreas().add( a ); + tx.commit(); + s.close(); + s = openSession(); + tx = s.beginTransaction(); + ip = (InspectorPrefixes) s.get( InspectorPrefixes.class, ip.getId() ); + assertNotNull( ip ); + assertEquals( 1, ip.getAreas().size() ); + assertEquals( a.getId(), ip.getAreas().get( 0 ).getId() ); + s.delete( ip ); + s.delete( ip.getAreas().get( 0 ) ); + tx.commit(); + s.close(); + } + + @Test + public void testJoinedSubclassManyToManyWithNonPkReference() throws Exception { + Session s = openSession(); + Zone a = new Zone(); + InspectorPrefixes ip = new InspectorPrefixes( "dgi" ); + ip.setName( "Inspector" ); + Transaction tx = s.beginTransaction(); + s.save( a ); + s.save( ip ); + ip.getDesertedAreas().add( a ); + tx.commit(); + s.close(); + s = openSession(); + tx = s.beginTransaction(); + ip = (InspectorPrefixes) s.get( InspectorPrefixes.class, ip.getId() ); + assertNotNull( ip ); + assertEquals( 1, ip.getDesertedAreas().size() ); + assertEquals( a.getId(), ip.getDesertedAreas().get( 0 ).getId() ); + s.delete( ip ); + s.delete( ip.getDesertedAreas().get( 0 ) ); + tx.commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Zone.class, + Inspector.class, + InspectorPrefixes.class, + }; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Zone.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Zone.java new file mode 100644 index 0000000000..3a93c418a4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/joinedsubclass/Zone.java @@ -0,0 +1,50 @@ +/* + * 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.test.annotations.manytomany.joinedsubclass; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author Emmanuel Bernard + */ +@Entity +@Table(name="`Zone`") +public class Zone { + private Integer id; + + @Id + @GeneratedValue + @Column(name = "id") + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Friend.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/Friend.java similarity index 50% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Friend.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/Friend.java index 7bf6a86271..1a3a28c785 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Friend.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/Friend.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.self; import java.io.Serializable; import java.util.Set; import javax.persistence.CascadeType; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/ManyToManySelfReferenceTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/ManyToManySelfReferenceTest.java new file mode 100644 index 0000000000..dd77718d35 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/self/ManyToManySelfReferenceTest.java @@ -0,0 +1,82 @@ +/* + * 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.test.annotations.manytomany.self; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +@FailureExpectedWithNewMetamodel +public class ManyToManySelfReferenceTest extends BaseCoreFunctionalTestCase { + + @Test + public void testSelf() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Friend f = new Friend(); + Friend sndF = new Friend(); + f.setName( "Starsky" ); + sndF.setName( "Hutch" ); + Set frnds = new HashSet(); + frnds.add( sndF ); + f.setFriends( frnds ); + //Starsky is a friend of Hutch but hutch is not + s.persist( f ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + f = (Friend) s.load( Friend.class, f.getId() ); + assertNotNull( f ); + assertNotNull( f.getFriends() ); + assertEquals( 1, f.getFriends().size() ); + Friend fromDb2ndFrnd = f.getFriends().iterator().next(); + assertEquals( fromDb2ndFrnd.getId(), sndF.getId() ); + assertEquals( 0, fromDb2ndFrnd.getFriends().size() ); + tx.commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Friend.class, + }; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/City.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/City.java new file mode 100644 index 0000000000..625ee9d143 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/City.java @@ -0,0 +1,56 @@ +/* + * 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.test.annotations.manytomany.simple; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author Emmanuel Bernard + */ +@Entity +@Table(name = "tbl_city") +public class City { + private Integer id; + private String name; + + @Id + @GeneratedValue + 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/test/java/org/hibernate/test/annotations/manytomany/simple/KnownClient.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/KnownClient.java new file mode 100644 index 0000000000..92133dba71 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/KnownClient.java @@ -0,0 +1,67 @@ +/* + * 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.test.annotations.manytomany.simple; + +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class KnownClient { + private Integer id; + private String name; + private Set stores; + + @Id + @GeneratedValue + 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; + } + + @ManyToMany(mappedBy = "customers") + public Set getStores() { + return stores; + } + + public void setStores(Set stores) { + this.stores = stores; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/ManyToManySimpleTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/ManyToManySimpleTest.java new file mode 100644 index 0000000000..62170385aa --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/ManyToManySimpleTest.java @@ -0,0 +1,176 @@ +/* + * 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.test.annotations.manytomany.simple; + +import java.util.HashSet; +import java.util.List; + +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.criterion.Restrictions; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +@FailureExpectedWithNewMetamodel +public class ManyToManySimpleTest extends BaseCoreFunctionalTestCase { + @Test + public void testDefault() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Store fnac = new Store(); + fnac.setName( "Fnac" ); + KnownClient emmanuel = new KnownClient(); + emmanuel.setName( "Emmanuel" ); + emmanuel.setStores( new HashSet() ); + fnac.setCustomers( new HashSet() ); + fnac.getCustomers().add( emmanuel ); + emmanuel.getStores().add( fnac ); + fnac.setImplantedIn( new HashSet() ); + City paris = new City(); + fnac.getImplantedIn().add( paris ); + paris.setName( "Paris" ); + s.persist( fnac ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + Store store; + KnownClient knownClient; + City city; + store = (Store) s.get( Store.class, fnac.getId() ); + assertNotNull( store ); + assertNotNull( store.getCustomers() ); + assertEquals( 1, store.getCustomers().size() ); + knownClient = store.getCustomers().iterator().next(); + assertEquals( emmanuel.getName(), knownClient.getName() ); + assertNotNull( store.getImplantedIn() ); + assertEquals( 1, store.getImplantedIn().size() ); + city = store.getImplantedIn().iterator().next(); + assertEquals( paris.getName(), city.getName() ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + knownClient = (KnownClient) s.get( KnownClient.class, emmanuel.getId() ); + assertNotNull( knownClient ); + assertNotNull( knownClient.getStores() ); + assertEquals( 1, knownClient.getStores().size() ); + store = knownClient.getStores().iterator().next(); + assertEquals( fnac.getName(), store.getName() ); + tx.commit(); + s.close(); + } + + @Test + public void testCanUseCriteriaQuery() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Store fnac = new Store(); + fnac.setName( "Fnac" ); + Supplier emi = new Supplier(); + emi.setName( "Emmanuel" ); + emi.setSuppStores( new HashSet() ); + fnac.setSuppliers( new HashSet() ); + fnac.getSuppliers().add( emi ); + emi.getSuppStores().add( fnac ); + s.persist( fnac ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + List result = s.createCriteria( Supplier.class ).createAlias( "suppStores", "s" ).add( + Restrictions.eq( "s.name", "Fnac" ) ).list(); + assertEquals( 1, result.size() ); + tx.commit(); + s.close(); + } + + @Test + public void testMappedBy() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Store fnac = new Store(); + fnac.setName( "Fnac" ); + Supplier emi = new Supplier(); + emi.setName( "Emmanuel" ); + emi.setSuppStores( new HashSet() ); + fnac.setSuppliers( new HashSet() ); + fnac.getSuppliers().add( emi ); + emi.getSuppStores().add( fnac ); + s.persist( fnac ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + Store store; + Supplier supplier; + store = (Store) s.get( Store.class, fnac.getId() ); + assertNotNull( store ); + assertNotNull( store.getSuppliers() ); + assertEquals( 1, store.getSuppliers().size() ); + supplier = store.getSuppliers().iterator().next(); + assertEquals( emi.getName(), supplier.getName() ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + supplier = (Supplier) s.get( Supplier.class, emi.getId() ); + assertNotNull( supplier ); + assertNotNull( supplier.getSuppStores() ); + assertEquals( 1, supplier.getSuppStores().size() ); + store = supplier.getSuppStores().iterator().next(); + assertEquals( fnac.getName(), store.getName() ); + tx.commit(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Store.class, + KnownClient.class, + Supplier.class, + City.class, + }; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Store.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Store.java similarity index 57% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Store.java rename to hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Store.java index 993ce4f80f..d307cdbcb2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/Store.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Store.java @@ -1,5 +1,27 @@ -//$Id$ -package org.hibernate.test.annotations.manytomany; +/* + * 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.test.annotations.manytomany.simple; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Supplier.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Supplier.java new file mode 100644 index 0000000000..ba54fe92df --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/simple/Supplier.java @@ -0,0 +1,67 @@ +/* + * 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.test.annotations.manytomany.simple; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class Supplier { + private Integer id; + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + private Set suppStores; + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @ManyToMany(mappedBy = "suppliers") + public Set getSuppStores() { + return suppStores; + } + + public void setSuppStores(Set suppStores) { + this.suppStores = suppStores; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employee.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employee.java new file mode 100644 index 0000000000..3220ba08b3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employee.java @@ -0,0 +1,80 @@ +/* + * 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.test.annotations.manytomany.targetentity; +import java.io.Serializable; +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToMany; + +import org.hibernate.annotations.Cascade; + +/** + * Employee in an Employer-Employee relationship + * + * @author Emmanuel Bernard + */ +@Entity +@SuppressWarnings("serial") +public class Employee implements Serializable { + private Integer id; + private Collection employers; + private String name; + + @Column(name="fld_name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setId(Integer integer) { + id = integer; + } + + @ManyToMany( + cascade = {CascadeType.PERSIST, CascadeType.MERGE}, + mappedBy = "employees" + ) + @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, + org.hibernate.annotations.CascadeType.PERSIST}) + public Collection getEmployers() { + return employers; + } + + public void setEmployers(Collection employers) { + this.employers = employers; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employer.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employer.java new file mode 100644 index 0000000000..4d27b0bcf5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/Employer.java @@ -0,0 +1,79 @@ +/* + * 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.test.annotations.manytomany.targetentity; +import java.io.Serializable; +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.OrderBy; +import javax.persistence.Table; + +import org.hibernate.annotations.Cascade; + +/** + * Employer in a employer-Employee relationship + * + * @author Emmanuel Bernard + */ +@Entity() +@Table(name="`Employer`") +@SuppressWarnings({"serial", "unchecked"}) +public class Employer implements Serializable { + private Integer id; + private Collection employees; + + @ManyToMany( + targetEntity = org.hibernate.test.annotations.manytomany.targetentity.Employee.class, + cascade = {CascadeType.PERSIST, CascadeType.MERGE} + ) + @JoinTable( + name = "EMPLOYER_EMPLOYEE", + joinColumns = {@JoinColumn(name = "EMPER_ID")}, + inverseJoinColumns = {@JoinColumn(name = "EMPEE_ID")} + ) + @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) + @OrderBy("name asc") + public Collection getEmployees() { + return employees; + } + + @Id + @GeneratedValue + public Integer getId() { + return id; + } + + public void setEmployees(Collection set) { + employees = set; + } + + public void setId(Integer integer) { + id = integer; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/ManyToManyTargetEntityTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/ManyToManyTargetEntityTest.java new file mode 100644 index 0000000000..46ddd04bbd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/manytomany/targetentity/ManyToManyTargetEntityTest.java @@ -0,0 +1,145 @@ +/* + * 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.test.annotations.manytomany.targetentity; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.junit.Test; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.FailureExpectedWithNewMetamodel; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Gail Badner (extracted from ManyToManyTest authored by Emmanuel Bernard) + */ +@FailureExpectedWithNewMetamodel +public class ManyToManyTargetEntityTest extends BaseCoreFunctionalTestCase { + @Test + public void testBasic() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Employer er = new Employer(); + Employee ee = new Employee(); + s.persist( ee ); + Set erColl = new HashSet(); + Collection eeColl = new ArrayList(); + erColl.add( ee ); + eeColl.add( er ); + er.setEmployees( erColl ); + ee.setEmployers( eeColl ); + //s.persist(ee); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + er = (Employer) s.load( Employer.class, er.getId() ); + assertNotNull( er ); + assertNotNull( er.getEmployees() ); + assertEquals( 1, er.getEmployees().size() ); + Employee eeFromDb = (Employee) er.getEmployees().iterator().next(); + assertEquals( ee.getId(), eeFromDb.getId() ); + tx.commit(); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + ee = (Employee) s.get( Employee.class, ee.getId() ); + assertNotNull( ee ); + assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); + tx.commit(); + assertFalse( "ManyToMany mappedBy lazyness", Hibernate.isInitialized( ee.getEmployers() ) ); + s.close(); + + s = openSession(); + tx = s.beginTransaction(); + ee = (Employee) s.get( Employee.class, ee.getId() ); + assertNotNull( ee ); + er = ee.getEmployers().iterator().next(); + assertTrue( "second join non lazy", Hibernate.isInitialized( er ) ); + s.delete( er ); + s.delete( ee ); + tx.commit(); + s.close(); + } + + @Test + public void testOrderByEmployee() throws Exception { + Session s; + Transaction tx; + s = openSession(); + tx = s.beginTransaction(); + Employer employer = new Employer(); + Employee employee1 = new Employee(); + employee1.setName( "Emmanuel" ); + Employee employee2 = new Employee(); + employee2.setName( "Alice" ); + s.persist( employee1 ); + s.persist( employee2 ); + Set erColl = new HashSet(); + Collection eeColl = new ArrayList(); + Collection eeColl2 = new ArrayList(); + erColl.add( employee1 ); + erColl.add( employee2 ); + eeColl.add( employer ); + eeColl2.add( employer ); + employer.setEmployees( erColl ); + employee1.setEmployers( eeColl ); + employee2.setEmployers( eeColl2 ); + + s.flush(); + s.clear(); + + employer = (Employer) s.get( Employer.class, employer.getId() ); + assertNotNull( employer ); + assertNotNull( employer.getEmployees() ); + assertEquals( 2, employer.getEmployees().size() ); + Employee eeFromDb = (Employee) employer.getEmployees().iterator().next(); + assertEquals( employee2.getName(), eeFromDb.getName() ); + tx.rollback(); + s.close(); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ + Employee.class, + Employer.class + }; + } + +}