下面列出了org.hibernate.classic.Session#beginTransaction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void create() {
Session session = getSessions().openSession();
session.beginTransaction();
Stock stock = new Stock();
stock.setTradeSymbol( "JBOSS" );
Valuation valuation = new Valuation();
valuation.setStock( stock );
valuation.setValuationDate( new Date( new java.util.Date().getTime() ) );
valuation.setValue( new Double( 200.0 ) );
stock.setCurrentValuation( valuation );
stock.getValuations().add( valuation );
session.save( stock );
session.save( valuation );
session.getTransaction().commit();
session.close();
stockId = stock.getId();
}
public void testCollectionOnly() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Mono m = new Mono();
Long id = (Long) s.save(m);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.update(m, id);
s.flush();
m.setAddress("foo bar");
s.flush();
s.delete(m);
t.commit();
s.close();
}
@Test
public void testSaveAdmin2(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Admin admin = new Admin();
admin.setName("cairou");
admin.setUsername("admin");
admin.setPwd("admin");
Authorization authorization = new Authorization();
authorization.setSuperSet(1);
authorization.setAdmin(admin);
admin.setAuthorization(authorization);
session.save(admin);
transaction.commit();
session.close();
}
private void initData() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Widget obj = new Widget();
obj.setValueOne(7);
obj.setValueTwo(8);
obj.setValueThree(9);
obj.setValueFour(10);
obj.setString("all-normal");
s.save(obj);
obj = new Widget();
obj.setValueOne(1);
obj.setValueTwo(2);
obj.setValueThree(-1);
obj.setValueFour(-5);
obj.setString("all-default");
s.save(obj);
t.commit();
s.close();
}
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 );
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 testInsertWithMismatchedTypes() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction t = s.beginTransaction();
try {
s.createQuery( "insert into Pickup (owner, vin, id) select id, vin, owner from Car" ).executeUpdate();
fail( "mismatched types did not error" );
}
catch( QueryException e ) {
// expected result
}
t.commit();
t = s.beginTransaction();
s.createQuery( "delete Vehicle" ).executeUpdate();
t.commit();
s.close();
data.cleanup();
}
public void testTempTableGenerationIsolation() throws Throwable{
Session s = openSession();
s.beginTransaction();
Truck truck = new Truck();
truck.setVin( "123t" );
truck.setOwner( "Steve" );
s.save( truck );
// manually flush the session to ensure the insert happens
s.flush();
// now issue a bulk delete against Car which should force the temp table to be
// created. we need to test to ensure that this does not cause the transaction
// to be committed...
s.createQuery( "delete from Vehicle" ).executeUpdate();
s.getTransaction().rollback();
s.close();
s = openSession();
s.beginTransaction();
List list = s.createQuery( "from Car" ).list();
assertEquals( "temp table gen caused premature commit", 0, list.size() );
s.createQuery( "delete from Car" ).executeUpdate();
s.getTransaction().rollback();
s.close();
}
@Test
public void testSaveReader(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Reader reader = new Reader();
// reader.setReaderId("123");
reader.setName("菜肉");
reader.setPwd("123");
//reader.setReaderType(1);
session.save(reader);
transaction.commit();
session.close();
}
public void testNarrow() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery("from E e join e.reverse as b where b.count=1").list();
s.createQuery("from E e join e.as as b where b.count=1").list();
t.commit();
s.close();
}
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 testMultiLevelCascade() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
Detail detail = new Detail();
SubDetail subdetail = new SubDetail();
Master m = new Master();
Master m0 = new Master();
Serializable m0id = s.save(m0);
m0.addDetail(detail);
detail.setMaster(m0);
m.getMoreDetails().add(detail);
detail.setSubDetails( new HashSet() );
detail.getSubDetails().add(subdetail);
Serializable mid = s.save(m);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
m = (Master) s.load( Master.class, mid );
assertTrue( ( (Detail) m.getMoreDetails().iterator().next() ).getSubDetails().size()!=0 );
s.delete(m);
assertTrue( s.find("from SubDetail").size()==0 );
assertTrue( s.find("from Detail d").size()==0 );
s.delete( s.load(Master.class, m0id) );
txn.commit();
s.close();
}
public void testAutoFlush() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
FooProxy foo = new Foo();
s.save(foo);
assertTrue( "autoflush create", s.find("from Foo foo").size()==1 );
foo.setChar( new Character('X') );
assertTrue( "autoflush update", s.find("from Foo foo where foo.char='X'").size()==1 );
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
foo = (FooProxy) s.load( Foo.class, foo.getKey() );
//s.update( new Foo(), foo.getKey() );
//assertTrue( s.find("from Foo foo where not foo.char='X'").size()==1, "autoflush update" );
if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof PointbaseDialect) ) {
foo.setBytes( "osama".getBytes() );
assertTrue( "autoflush collection update", s.find("from Foo foo where 111 in elements(foo.bytes)").size()==1 );
foo.getBytes()[0] = 69;
assertTrue( "autoflush collection update", s.find("from Foo foo where 69 in elements(foo.bytes)").size()==1 );
}
s.delete(foo);
assertTrue( "autoflush delete", s.find("from Foo foo").size()==0 );
txn.commit();
s.close();
}
public void testDeleteNonExistentEntity() {
Session s = openSession();
Transaction t = s.beginTransaction();
try {
s.createQuery( "delete NonExistentEntity" ).executeUpdate();
fail( "no exception thrown" );
}
catch( QueryException e ) {
log.debug( "Caught expected error type : " + e.getMessage() );
}
t.commit();
s.close();
}
@Test
public void testSaveBorrow(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
BorrowInfo borrowInfo = new BorrowInfo();
Admin admin = new Admin();
admin.setAid(2);
borrowInfo.setAdmin(admin);
session.save(borrowInfo);
transaction.commit();
session.close();
}
public void testNonLazyBidirectional() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Single sin = new Single();
sin.setId("asdfds");
sin.setString("adsa asdfasd");
Several sev = new Several();
sev.setId("asdfasdfasd");
sev.setString("asd ddd");
sin.getSeveral().add(sev);
sev.setSingle(sin);
s.save(sin);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
sin = (Single) s.load( Single.class, sin );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
sev = (Several) s.load( Several.class, sev );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.find("from Several");
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete("from Single");
t.commit();
s.close();
}
public void testTS() throws Exception {
if (getDialect() instanceof Oracle9Dialect) return;
Session session = openSession();
Transaction txn = session.beginTransaction();
Simple sim = new Simple();
sim.setDate( new Date() );
session.save( sim, new Long(1) );
Query q = session.createSQLQuery("select {sim.*} from Simple {sim} where {sim}.date_ = ?", "sim", Simple.class);
q.setTimestamp( 0, sim.getDate() );
assertTrue ( q.list().size()==1 );
session.delete(sim);
txn.commit();
session.close();
}
public void testSharedColumn() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
C1 c1 = new C1();
C2 c2 = new C2();
c1.setC2(c2);
c2.setC1(c1);
s.save(c1); s.save(c2);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
List list = s.find("from B");
assertTrue( list.size()==2 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c1 = (C1) s.createQuery("from C1").uniqueResult();
c2 = (C2) s.createQuery("from C2").uniqueResult();
assertTrue( c1.getC2()==c2 );
assertTrue( c2.getC1()==c1 );
assertTrue( c1.getC2s().contains(c2) );
assertTrue( c2.getC1s().contains(c1) );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c1 = (C1) s.get( A.class, c1.getId() );
c2 = (C2) s.get( A.class, c2.getId() );
assertTrue( c1.getC2()==c2 );
assertTrue( c2.getC1()==c1 );
assertTrue( c1.getC2s().contains(c2) );
assertTrue( c2.getC1s().contains(c1) );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete(c1); s.delete(c2);
t.commit();
s.close();
}
public void testInsertWithGeneratedId() {
// Make sure the env supports bulk inserts with generated ids...
EntityPersister persister = sfi().getEntityPersister( PettingZoo.class.getName() );
IdentifierGenerator generator = persister.getIdentifierGenerator();
if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) {
return;
}
// create a Zoo
Zoo zoo = new Zoo();
zoo.setName( "zoo" );
Session s = openSession();
Transaction t = s.beginTransaction();
s.save( zoo );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
int count = s.createQuery( "insert into PettingZoo (name) select name from Zoo" ).executeUpdate();
t.commit();
s.close();
assertEquals( "unexpected insertion count", 1, count );
s = openSession();
t = s.beginTransaction();
PettingZoo pz = ( PettingZoo ) s.createQuery( "from PettingZoo" ).uniqueResult();
t.commit();
s.close();
assertEquals( zoo.getName(), pz.getName() );
assertTrue( zoo.getId() != pz.getId() );
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete Zoo" ).executeUpdate();
t.commit();
s.close();
}
public void testBagMultipleElements() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Baz baz = new Baz();
baz.setBag( new ArrayList() );
baz.setByteBag( new ArrayList() );
s.save(baz);
baz.getBag().add("foo");
baz.getBag().add("bar");
baz.getByteBag().add( "foo".getBytes() );
baz.getByteBag().add( "bar".getBytes() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
//put in cache
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertTrue( baz.getBag().size()==2 );
assertTrue( baz.getByteBag().size()==2 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertTrue( baz.getBag().size()==2 );
assertTrue( baz.getByteBag().size()==2 );
baz.getBag().remove("bar");
baz.getBag().add("foo");
baz.getByteBag().add( "bar".getBytes() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
baz = (Baz) s.get( Baz.class, baz.getCode() );
assertTrue( baz.getBag().size()==2 );
assertTrue( baz.getByteBag().size()==3 );
s.delete(baz);
t.commit();
s.close();
}