📄 manytomanytest.java
字号:
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(); } 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 = (Friend) f.getFriends().iterator().next(); assertEquals( fromDb2ndFrnd.getId(), sndF.getId() ); assertEquals( 0, fromDb2ndFrnd.getFriends().size() ); tx.commit(); s.close(); } 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<Woman> womens = new HashSet<Woman>(); womens.add( w1 ); m1.setWomens( womens ); Set<Woman> womens2 = new HashSet<Woman>(); womens2.add( w1 ); womens2.add( w2 ); m2.setWomens( womens2 ); Set<Man> mens = new HashSet<Man>(); mens.add( m1 ); mens.add( m2 ); w1.setMens( mens ); Set<Man> mens2 = new HashSet<Man>(); 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(); } public void testAssociationTableUniqueConstraints() throws Exception { Session s = openSession(); Permission readAccess = new Permission(); readAccess.setPermission( "read" ); readAccess.setExpirationDate( new Date() ); Collection<Permission> coll = new ArrayList<Permission>( 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(); } } 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<Permission> coll = new ArrayList<Permission>( 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(); } 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<Permission> coll = new HashSet<Permission>( 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<Permission> permIter = group.getPermissions().iterator(); assertEquals( "write", permIter.next().getPermission() ); assertEquals( "execute", permIter.next().getPermission() ); assertEquals( "read", permIter.next().getPermission() ); s.getTransaction().rollback(); s.close(); } 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(); } 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(); } 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(); } /** * @see org.hibernate.test.annotations.TestCase#getMappings() */ protected Class[] getMappings() { return new Class[]{ Friend.class, Employer.class, Employee.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 }; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -