📄 bulkmanipulationtest.java
字号:
// Make sure the env supports bulk inserts with generated ids... EntityPersister persister = sfi().getEntityPersister( TimestampVersioned.class.getName() ); IdentifierGenerator generator = persister.getIdentifierGenerator(); if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) { return; } Session s = openSession(); Transaction t = s.beginTransaction(); TimestampVersioned entity = new TimestampVersioned( "int-vers" ); s.save( entity ); s.createQuery( "select id, name, version from TimestampVersioned" ).list(); t.commit(); s.close(); Long initialId = entity.getId(); //Date initialVersion = entity.getVersion(); s = openSession(); t = s.beginTransaction(); int count = s.createQuery( "insert into TimestampVersioned ( name ) select name from TimestampVersioned" ).executeUpdate(); t.commit(); s.close(); assertEquals( "unexpected insertion count", 1, count ); s = openSession(); t = s.beginTransaction(); TimestampVersioned created = ( TimestampVersioned ) s.createQuery( "from TimestampVersioned where id <> :initialId" ) .setLong( "initialId", initialId.longValue() ) .uniqueResult(); t.commit(); s.close(); assertNotNull( created.getVersion() ); //assertEquals( "version was not seeded", initialVersion, created.getVersion() ); s = openSession(); t = s.beginTransaction(); s.createQuery( "delete TimestampVersioned" ).executeUpdate(); t.commit(); s.close(); } public void testInsertWithSelectListUsingJoins() { // this is just checking parsing and syntax... Session s = openSession(); s.beginTransaction(); s.createQuery( "insert into Animal (description, bodyWeight) select h.description, h.bodyWeight from Human h where h.mother.mother is not null" ).executeUpdate(); s.createQuery( "insert into Animal (description, bodyWeight) select h.description, h.bodyWeight from Human h join h.mother m where m.mother is not null" ).executeUpdate(); s.createQuery( "delete from Animal" ).executeUpdate(); s.getTransaction().commit(); s.close(); } // UPDATES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public void testIncorrectSyntax() { Session s = openSession(); Transaction t = s.beginTransaction(); try { s.createQuery( "update Human set Human.description = 'xyz' where Human.id = 1 and Human.description is null" ); fail( "expected failure" ); } catch( QueryException expected ) { // ignore : expected behavior } t.commit(); s.close(); } public void testUpdateWithWhereExistsSubquery() { // multi-table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Session s = openSession(); Transaction t = s.beginTransaction(); Human joe = new Human(); joe.setName( new Name( "Joe", 'Q', "Public" ) ); s.save( joe ); Human doll = new Human(); doll.setName( new Name( "Kyu", 'P', "Doll" ) ); doll.setFriends( new ArrayList() ); doll.getFriends().add( joe ); s.save( doll ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); String updateQryString = "update Human h " + "set h.description = 'updated' " + "where exists (" + " select f.id " + " from h.friends f " + " where f.name.last = 'Public' " + ")"; int count = s.createQuery( updateQryString ).executeUpdate(); assertEquals( 1, count ); s.delete( doll ); s.delete( joe ); t.commit(); s.close(); // single-table (one-to-many & many-to-many) ~~~~~~~~~~~~~~~~~~~~~~~~~~ s = openSession(); t = s.beginTransaction(); SimpleEntityWithAssociation entity = new SimpleEntityWithAssociation(); SimpleEntityWithAssociation other = new SimpleEntityWithAssociation(); entity.setName( "main" ); other.setName( "many-to-many-association" ); entity.getManyToManyAssociatedEntities().add( other ); entity.addAssociation( "one-to-many-association" ); s.save( entity ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); // one-to-many test updateQryString = "update SimpleEntityWithAssociation e " + "set e.name = 'updated' " + "where exists (" + " select a.id " + " from e.associatedEntities a " + " where a.name = 'one-to-many-association' " + ")"; count = s.createQuery( updateQryString ).executeUpdate(); assertEquals( 1, count ); // many-to-many test if ( supportsSubqueryOnMutatingTable() ) { updateQryString = "update SimpleEntityWithAssociation e " + "set e.name = 'updated' " + "where exists (" + " select a.id " + " from e.manyToManyAssociatedEntities a " + " where a.name = 'many-to-many-association' " + ")"; count = s.createQuery( updateQryString ).executeUpdate(); assertEquals( 1, count ); } s.delete( entity.getManyToManyAssociatedEntities().iterator().next() ); s.delete( entity ); t.commit(); s.close(); } public void testIncrementCounterVersion() { Session s = openSession(); Transaction t = s.beginTransaction(); IntegerVersioned entity = new IntegerVersioned( "int-vers" ); s.save( entity ); t.commit(); s.close(); int initialVersion = entity.getVersion(); s = openSession(); t = s.beginTransaction(); int count = s.createQuery( "update versioned IntegerVersioned set name = name" ).executeUpdate(); assertEquals( "incorrect exec count", 1, count ); t.commit(); t = s.beginTransaction(); entity = ( IntegerVersioned ) s.load( IntegerVersioned.class, entity.getId() ); assertEquals( "version not incremented", initialVersion + 1, entity.getVersion() ); s.delete( entity ); t.commit(); s.close(); } public void testIncrementTimestampVersion() { Session s = openSession(); Transaction t = s.beginTransaction(); TimestampVersioned entity = new TimestampVersioned( "ts-vers" ); s.save( entity ); t.commit(); s.close(); Date initialVersion = entity.getVersion(); synchronized (this) { try { wait(1500); } catch (InterruptedException ie) {} } s = openSession(); t = s.beginTransaction(); int count = s.createQuery( "update versioned TimestampVersioned set name = name" ).executeUpdate(); assertEquals( "incorrect exec count", 1, count ); t.commit(); t = s.beginTransaction(); entity = ( TimestampVersioned ) s.load( TimestampVersioned.class, entity.getId() ); assertTrue( "version not incremented", entity.getVersion().after( initialVersion ) ); s.delete( entity ); t.commit(); s.close(); } public void testUpdateOnComponent() { Session s = openSession(); Transaction t = s.beginTransaction(); Human human = new Human(); human.setName( new Name( "Stevee", 'X', "Ebersole" ) ); s.save( human ); s.flush(); t.commit(); String correctName = "Steve"; t = s.beginTransaction(); int count = s.createQuery( "update Human set name.first = :correction where id = :id" ) .setString( "correction", correctName ) .setLong( "id", human.getId().longValue() ) .executeUpdate(); assertEquals( "Incorrect update count", 1, count ); t.commit(); t = s.beginTransaction(); s.refresh( human ); assertEquals( "Update did not execute properly", correctName, human.getName().getFirst() ); s.createQuery( "delete Human" ).executeUpdate(); t.commit(); s.close(); } public void testUpdateOnManyToOne() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "update Animal a set a.mother = null where a.id = 2" ).executeUpdate(); if ( ! ( getDialect() instanceof MySQLDialect ) ) { // MySQL does not support (even un-correlated) subqueries against the update-mutating table s.createQuery( "update Animal a set a.mother = (from Animal where id = 1) where a.id = 2" ).executeUpdate(); } t.commit(); s.close(); } public void testUpdateOnImplicitJoinFails() { Session s = openSession(); Transaction t = s.beginTransaction(); Human human = new Human(); human.setName( new Name( "Steve", 'E', null ) ); Human mother = new Human(); mother.setName( new Name( "Jane", 'E', null ) ); human.setMother( mother ); s.save( human ); s.save( mother ); s.flush(); t.commit(); t = s.beginTransaction(); try { s.createQuery( "update Human set mother.name.initial = :initial" ).setString( "initial", "F" ).executeUpdate(); fail( "update allowed across implicit join" ); } catch( QueryException e ) { log.debug( "TEST (OK) : " + e.getMessage() ); // expected condition } s.createQuery( "delete Human where mother is not null" ).executeUpdate(); s.createQuery( "delete Human" ).executeUpdate(); t.commit(); s.close(); } public void testUpdateOnDiscriminatorSubclass() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "update PettingZoo set name = name" ).executeUpdate(); assertEquals( "Incorrect discrim subclass update count", 1, count ); t.rollback(); t = s.beginTransaction(); count = s.createQuery( "update PettingZoo pz set pz.name = pz.name where pz.id = :id" ) .setLong( "id", data.pettingZoo.getId().longValue() ) .executeUpdate(); assertEquals( "Incorrect discrim subclass update count", 1, count ); t.rollback(); t = s.beginTransaction(); count = s.createQuery( "update Zoo as z set z.name = z.name" ).executeUpdate(); assertEquals( "Incorrect discrim subclass update count", 2, count ); t.rollback(); t = s.beginTransaction(); // TODO : not so sure this should be allowed. Seems to me that if they specify an alias, // property-refs should be required to be qualified. count = s.createQuery( "update Zoo as z set name = name where id = :id" ) .setLong( "id", data.zoo.getId().longValue() ) .executeUpdate(); assertEquals( "Incorrect discrim subclass update count", 1, count ); t.commit(); s.close(); data.cleanup(); } public void testUpdateOnAnimal() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "update Animal set description = description where description = :desc" ) .setString( "desc", data.frog.getDescription() ) .executeUpdate(); assertEquals( "Incorrect entity-updated count", 1, count ); count = s.createQuery( "update Animal set description = :newDesc where description = :desc" ) .setString( "desc", data.polliwog.getDescription() ) .setString( "newDesc", "Tadpole" ) .executeUpdate(); assertEquals( "Incorrect entity-updated count", 1, count ); Animal tadpole = ( Animal ) s.load( Animal.class, data.polliwog.getId() ); assertEquals( "Update did not take effect", "Tadpole", tadpole.getDescription() ); count = s.createQuery( "update Animal set bodyWeight = bodyWeight + :w1 + :w2" ) .setDouble( "w1", 1 ) .setDouble( "w2", 2 ) .executeUpdate(); assertEquals( "incorrect count on 'complex' update assignment", count, 6 ); if ( ! ( getDialect() instanceof MySQLDialect ) ) { // MySQL does not support (even un-correlated) subqueries against the update-mutating table s.createQuery( "update Animal set bodyWeight = ( select max(bodyWeight) from Animal )" ) .executeUpdate(); } t.commit(); s.close(); data.cleanup(); } public void testUpdateOnMammal() { TestData data = new TestData(); data.prepare(); Session s = openSession(); Transaction t = s.beginTransaction(); int count = s.createQuery( "update Mammal set description = description" ).executeUpdate(); assertEquals( "incorrect update count against 'middle' of joined-subclass hierarchy", 2, count ); count = s.createQuery( "update Mammal set bodyWeight = 25" ).executeUpdate(); assertEquals( "incorrect update count against 'middle' of joined-subclass hierarchy", 2, count ); if ( ! ( getDialect() instanceof MySQLDialect ) ) { // MySQL does not support (even un-correlated) subqueries against the update-mutating table count = s.createQuery( "update Mammal set bodyWeight = ( select max(bodyWeight) from Animal )" ).executeUpdate(); assertEquals( "incorrect update count against 'middle' of joined-subclass hierarchy", 2, count ); } t.commit(); s.close(); data.cleanup(); } public void testUpdateSetNullUnionSubclass() { 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( "update Vehicle set owner = 'Steve'" ).executeUpdate(); assertEquals( "incorrect restricted update count", 4, count ); count = s.createQuery( "update Vehicle set owner = null where owner = 'Steve'" ).executeUpdate(); assertEquals( "incorrect restricted update count", 4, count ); count = s.createQuery( "delete Vehicle where owner is null" ).executeUpdate(); assertEquals( "incorrect restricted update count", 4, count );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -