📄 bulkmanipulationtest.java
字号:
assertEquals( "incorrect restricted update count", 4, count ); count = s.createQuery( "delete Vehicle where owner is null" ).executeUpdate(); assertEquals( "incorrect restricted update count", 4, count ); t.commit(); s.close(); data.cleanup(); } public void testUpdateSetNullOnDiscriminatorSubclass() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "update PettingZoo set address.city = null" ).executeUpdate(); assertEquals( "Incorrect discrim subclass delete count", 1, count ); count = s.createQuery( "delete Zoo where address.city is null" ).executeUpdate(); assertEquals( "Incorrect discrim subclass delete count", 1, count ); count = s.createQuery( "update Zoo set address.city = null" ).executeUpdate(); assertEquals( "Incorrect discrim subclass delete count", 1, count ); count = s.createQuery( "delete Zoo where address.city is null" ).executeUpdate(); assertEquals( "Incorrect discrim subclass delete count", 1, count ); t.commit(); s.close(); data.cleanup(); } public void testUpdateSetNullOnJoinedSubclass() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "update Mammal set bodyWeight = null" ).executeUpdate(); assertEquals( "Incorrect deletion count on joined subclass", 2, count ); count = s.createQuery( "delete Animal where bodyWeight = null" ).executeUpdate(); assertEquals( "Incorrect deletion count on joined subclass", 2, count ); t.commit(); s.close(); data.cleanup(); } // DELETES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public void testDeleteWithSubquery() { // setup the test data... Session s = openSession(); s.beginTransaction(); SimpleEntityWithAssociation owner = new SimpleEntityWithAssociation( "myEntity-1" ); owner.addAssociation( "assoc-1" ); owner.addAssociation( "assoc-2" ); owner.addAssociation( "assoc-3" ); s.save( owner ); SimpleEntityWithAssociation owner2 = new SimpleEntityWithAssociation( "myEntity-2" ); owner2.addAssociation( "assoc-1" ); owner2.addAssociation( "assoc-2" ); owner2.addAssociation( "assoc-3" ); owner2.addAssociation( "assoc-4" ); s.save( owner2 ); SimpleEntityWithAssociation owner3 = new SimpleEntityWithAssociation( "myEntity-3" ); s.save( owner3 ); s.getTransaction().commit(); s.close(); // now try the bulk delete s = openSession(); s.beginTransaction(); int count = s.createQuery( "delete SimpleEntityWithAssociation e where size( e.associatedEntities ) = 0 and e.name like '%'" ).executeUpdate(); assertEquals( "incorrect delete count", 1, count ); s.getTransaction().commit(); s.close(); // finally, clean up s = openSession(); s.beginTransaction(); s.createQuery( "delete SimpleAssociatedEntity" ).executeUpdate(); s.createQuery( "delete SimpleEntityWithAssociation" ).executeUpdate(); s.getTransaction().commit(); s.close(); } public void testSimpleDeleteOnAnimal() { if ( getDialect().hasSelfReferentialForeignKeyBug() ) { reportSkip( "self referential FK bug", "HQL delete testing" ); return; } TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete from Animal as a where a.id = :id" ) .setLong( "id", data.polliwog.getId().longValue() ) .executeUpdate(); assertEquals( "Incorrect delete count", 1, count ); count = s.createQuery( "delete Animal where id = :id" ) .setLong( "id", data.catepillar.getId().longValue() ) .executeUpdate(); assertEquals( "incorrect delete count", 1, count ); // HHH-873... if ( supportsSubqueryOnMutatingTable() ) { count = s.createQuery( "delete from User u where u not in (select u from User u)" ).executeUpdate(); assertEquals( 0, count ); } count = s.createQuery( "delete Animal a" ).executeUpdate(); assertEquals( "Incorrect delete count", 4, count ); List list = s.createQuery( "select a from Animal as a" ).list(); assertTrue( "table not empty", list.isEmpty() ); t.commit(); s.close(); data.cleanup(); } public void testDeleteOnDiscriminatorSubclass() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete PettingZoo" ).executeUpdate(); assertEquals( "Incorrect discrim subclass delete count", 1, count ); count = s.createQuery( "delete Zoo" ).executeUpdate(); assertEquals( "Incorrect discrim subclass delete count", 1, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteOnJoinedSubclass() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete Mammal where bodyWeight > 150" ).executeUpdate(); assertEquals( "Incorrect deletion count on joined subclass", 1, count ); count = s.createQuery( "delete Mammal" ).executeUpdate(); assertEquals( "Incorrect deletion count on joined subclass", 1, count ); count = s.createQuery( "delete SubMulti" ).executeUpdate(); assertEquals( "Incorrect deletion count on joined subclass", 0, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteOnMappedJoin() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete Joiner where joinedName = :joinedName" ).setString( "joinedName", "joined-name" ).executeUpdate(); assertEquals( "Incorrect deletion count on joined subclass", 1, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteUnionSubclassAbstractRoot() { TestData data = new TestData(); data.prepare(); // These should reach out into *all* subclass tables... Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete Vehicle where owner = :owner" ).setString( "owner", "Steve" ).executeUpdate(); assertEquals( "incorrect restricted update count", 1, count ); count = s.createQuery( "delete Vehicle" ).executeUpdate(); assertEquals( "incorrect update count", 3, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteUnionSubclassConcreteSubclass() { TestData data = new TestData(); data.prepare(); // These should only affect the given table Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete Truck where owner = :owner" ).setString( "owner", "Steve" ).executeUpdate(); assertEquals( "incorrect restricted update count", 1, count ); count = s.createQuery( "delete Truck" ).executeUpdate(); assertEquals( "incorrect update count", 2, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteUnionSubclassLeafSubclass() { TestData data = new TestData(); data.prepare(); // These should only affect the given table Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete Car where owner = :owner" ).setString( "owner", "Kirsten" ).executeUpdate(); assertEquals( "incorrect restricted update count", 1, count ); count = s.createQuery( "delete Car" ).executeUpdate(); assertEquals( "incorrect update count", 0, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteWithMetadataWhereFragments() throws Throwable { Session s = openSession(); Transaction t = s.beginTransaction(); // Note: we are just checking the syntax here... s.createQuery("delete from Bar").executeUpdate(); s.createQuery("delete from Bar where barString = 's'").executeUpdate(); t.commit(); s.close(); } public void testDeleteRestrictedOnManyToOne() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "delete Animal where mother = :mother" ) .setEntity( "mother", data.butterfly ) .executeUpdate(); assertEquals( 1, count ); t.commit(); s.close(); data.cleanup(); } public void testDeleteSyntaxWithCompositeId() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "delete EntityWithCrazyCompositeKey where id.id = 1 and id.otherId = 2" ).executeUpdate(); s.createQuery( "delete from EntityWithCrazyCompositeKey where id.id = 1 and id.otherId = 2" ).executeUpdate(); s.createQuery( "delete from EntityWithCrazyCompositeKey e where e.id.id = 1 and e.id.otherId = 2" ).executeUpdate(); t.commit(); s.close(); } private class TestData { private Animal polliwog; private Animal catepillar; private Animal frog; private Animal butterfly; private Zoo zoo; private Zoo pettingZoo; private void prepare() { Session s = openSession(); Transaction txn = s.beginTransaction(); polliwog = new Animal(); polliwog.setBodyWeight( 12 ); polliwog.setDescription( "Polliwog" ); catepillar = new Animal(); catepillar.setBodyWeight( 10 ); catepillar.setDescription( "Catepillar" ); frog = new Animal(); frog.setBodyWeight( 34 ); frog.setDescription( "Frog" ); polliwog.setFather( frog ); frog.addOffspring( polliwog ); butterfly = new Animal(); butterfly.setBodyWeight( 9 ); butterfly.setDescription( "Butterfly" ); catepillar.setMother( butterfly ); butterfly.addOffspring( catepillar ); s.save( frog ); s.save( polliwog ); s.save( butterfly ); s.save( catepillar ); Dog dog = new Dog(); dog.setBodyWeight( 200 ); dog.setDescription( "dog" ); s.save( dog ); Cat cat = new Cat(); cat.setBodyWeight( 100 ); cat.setDescription( "cat" ); s.save( cat ); zoo = new Zoo(); zoo.setName( "Zoo" ); Address add = new Address(); add.setCity("MEL"); add.setCountry("AU"); add.setStreet("Main st"); add.setPostalCode("3000"); zoo.setAddress(add); pettingZoo = new PettingZoo(); pettingZoo.setName( "Petting Zoo" ); Address addr = new Address(); addr.setCity("Sydney"); addr.setCountry("AU"); addr.setStreet("High st"); addr.setPostalCode("2000"); pettingZoo.setAddress(addr); s.save( zoo ); s.save( pettingZoo ); Joiner joiner = new Joiner(); joiner.setJoinedName( "joined-name" ); joiner.setName( "name" ); s.save( joiner ); Car car = new Car(); car.setVin( "123c" ); car.setOwner( "Kirsten" ); s.save( car ); Truck truck = new Truck(); truck.setVin( "123t" ); truck.setOwner( "Steve" ); s.save( truck ); SUV suv = new SUV(); suv.setVin( "123s" ); suv.setOwner( "Joe" ); s.save( suv ); Pickup pickup = new Pickup(); pickup.setVin( "123p" ); pickup.setOwner( "Cecelia" ); s.save( pickup ); BooleanLiteralEntity bool = new BooleanLiteralEntity(); s.save( bool ); txn.commit(); s.close(); } private void cleanup() { Session s = openSession(); Transaction txn = s.beginTransaction(); // workaround awesome HSQLDB "feature" s.createQuery( "delete from Animal where mother is not null or father is not null" ).executeUpdate(); s.createQuery( "delete from Animal" ).executeUpdate(); s.createQuery( "delete from Zoo" ).executeUpdate(); s.createQuery( "delete from Joiner" ).executeUpdate(); s.createQuery( "delete from Vehicle" ).executeUpdate(); s.createQuery( "delete from BooleanLiteralEntity" ).executeUpdate(); txn.commit(); s.close(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -