📄 manytomanytest.java
字号:
//$Id: ManyToManyTest.java 15025 2008-08-11 09:14:39Z hardy.ferentschik $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.hibernate.Hibernate;import org.hibernate.JDBCException;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.dialect.HSQLDialect;import org.hibernate.test.annotations.RequiresDialect;import org.hibernate.test.annotations.TestCase;/** * Many to many tests * * @author Emmanuel Bernard */@SuppressWarnings("unchecked")public class ManyToManyTest extends TestCase { public ManyToManyTest(String x) { super( x ); } 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<Store>() ); fnac.setCustomers( new HashSet<KnownClient>() ); fnac.getCustomers().add( emmanuel ); emmanuel.getStores().add( fnac ); fnac.setImplantedIn( new HashSet<City>() ); 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 = (KnownClient) store.getCustomers().iterator().next(); assertEquals( emmanuel.getName(), knownClient.getName() ); assertNotNull( store.getImplantedIn() ); assertEquals( 1, store.getImplantedIn().size() ); city = (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 = (Store) knownClient.getStores().iterator().next(); assertEquals( fnac.getName(), store.getName() ); tx.commit(); s.close(); } @RequiresDialect(HSQLDialect.class) 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<Cat>() ); woman.getCats().add( cat ); cat.setHumanContacts( new HashSet<Woman>() ); 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(); } 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<Store>() ); fnac.setSuppliers( new HashSet<Supplier>() ); 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 = (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 = (Store) supplier.getSuppStores().iterator().next(); assertEquals( fnac.getName(), store.getName() ); tx.commit(); s.close(); } 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(); } 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(); } /** * ANN-625 * * @throws Exception in case the test fails. * * This fails test fails for other databases (except HSQL) due to missing alias in order by clause: * * select * contractor0_.EMPLOYER_ID as EMPLOYER1_1_, * contractor0_.CONTRACTOR_ID as CONTRACTOR2_1_, * contractor1_.id as id2_0_, * contractor1_.fld_name as fld3_2_0_, * contractor1_.hourlyRate as hourlyRate2_0_ * from * EMPLOYER_CONTRACTOR contractor0_ * left outer join * Employee contractor1_ * on contractor0_.CONTRACTOR_ID=contractor1_.id * where * contractor0_.EMPLOYER_ID=? * order by * Employee.fld_name desc * * */ @RequiresDialect(HSQLDialect.class) 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(); } public void testRemoveInBetween() throws Exception { Session s; Transaction tx;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -