Java Code Examples for org.hibernate.classic.Session#close()
The following examples show how to use
org.hibernate.classic.Session#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testPersistentLifecycle() throws Exception { Session s = openSession(); Qux q = new Qux(); s.save(q); q.setStuff("foo bar baz qux"); s.flush(); s.connection().commit(); s.close(); s = openSession(); q = (Qux) s.load( Qux.class, q.getKey() ); assertTrue( "lifecycle create", q.getCreated() ); assertTrue( "lifecycle load", q.getLoaded() ); assertTrue( "lifecycle subobject", q.getFoo()!=null ); s.delete(q); assertTrue( "lifecycle delete", q.getDeleted() ); s.flush(); s.connection().commit(); s.close(); s = openSession(); assertTrue( "subdeletion", s.find("from Foo foo").size()==0); s.flush(); s.connection().commit(); s.close(); }
Example 2
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); }
Example 3
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testCriteriaCollection() throws Exception { Session s = openSession(); Baz bb = (Baz) s.createCriteria(Baz.class).uniqueResult(); assertTrue(bb==null); Baz baz = new Baz(); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); Baz b = (Baz) s.createCriteria(Baz.class).uniqueResult(); assertTrue( Hibernate.isInitialized( b.getTopGlarchez() ) ); assertTrue( b.getTopGlarchez().size()==0 ); s.delete(b); s.flush(); s.connection().commit(); s.close(); }
Example 4
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); }
Example 5
Source File: ParentChildTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testQueryOneToOne() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Serializable id = s.save( new Parent() ); assertTrue( s.find("from Parent p left join fetch p.child").size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Parent p = (Parent) s.createQuery("from Parent p left join fetch p.child").uniqueResult(); assertTrue( p.getChild()==null ); s.find("from Parent p join p.child c where c.x > 0"); s.find("from Child c join c.parent p where p.x > 0"); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete( s.get(Parent.class, id) ); t.commit(); s.close(); }
Example 6
Source File: MasterDetailTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testNoUpdateManyToOne() throws Exception { Session s = openSession(); W w1 = new W(); W w2 = new W(); Z z = new Z(); z.setW(w1); s.save(z); s.flush(); z.setW(w2); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.update(z); s.flush(); s.delete(z); s.delete("from W"); s.flush(); s.connection().commit(); s.close(); }
Example 7
Source File: MultiTableTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testOneToOne() throws Exception { Session s = openSession(); Lower ls = new Lower(); Serializable id = s.save(ls); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.load(Lower.class, id); s.connection().commit(); s.close(); s = openSession(); s.delete( s.load(Lower.class, id) ); s.flush(); s.connection().commit(); s.close(); }
Example 8
Source File: MasterDetailTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testNotNullDiscriminator() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Up up = new Up(); up.setId1("foo"); up.setId2(123l); Down down = new Down(); down.setId1("foo"); down.setId2(321l); down.setValue(12312312l); s.save(up); s.save(down); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); List list = s.find("from Up up order by up.id2 asc"); assertTrue( list.size()==2 ); assertFalse( list.get(0) instanceof Down ); assertTrue( list.get(1) instanceof Down ); list = s.find("from Down down"); assertTrue( list.size()==1 ); assertTrue( list.get(0) instanceof Down ); //list = s.find("from Up down where down.class = Down"); assertTrue( list.size()==1 ); assertTrue( list.get(0) instanceof Down ); s.delete("from Up up"); t.commit(); s.close(); }
Example 9
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testDyna() throws Exception { Session s = openSession(); GlarchProxy g = new Glarch(); g.setName("G"); Serializable id = s.save(g); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, id); assertTrue( g.getName().equals("G") ); assertTrue( g.getDynaBean().get("foo").equals("foo") && g.getDynaBean().get("bar").equals( new Integer(66) ) ); assertTrue( ! (g instanceof Glarch) ); g.getDynaBean().put("foo", "bar"); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, id); assertTrue( g.getDynaBean().get("foo").equals("bar") && g.getDynaBean().get("bar").equals( new Integer(66) ) ); g.setDynaBean(null); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, id); assertTrue( g.getDynaBean()==null ); s.delete(g); s.flush(); s.connection().commit(); s.close(); }
Example 10
Source File: ParentChildTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testClassWhereManyToMany() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Baz baz = new Baz(); baz.setMoreParts( new ArrayList() ); Part p1 = new Part(); p1.setDescription("xyz"); Part p2 = new Part(); p2.setDescription("abc"); baz.getMoreParts().add(p1); baz.getMoreParts().add(p2); s.save(baz); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); assertTrue( s.createCriteria(Part.class).list().size()==1 ); //there is a where condition on Part mapping assertTrue( s.createCriteria(Part.class).add( Expression.eq( "id", p1.getId() ) ).list().size()==1 ); assertTrue( s.createQuery("from Part").list().size()==1 ); assertTrue( s.createQuery("from Baz baz join baz.moreParts").list().size()==2 ); baz = (Baz) s.createCriteria(Baz.class).uniqueResult(); assertTrue( s.createFilter( baz.getMoreParts(), "" ).list().size()==2 ); //assertTrue( baz.getParts().size()==1 ); s.delete( s.get( Part.class, p1.getId() )); s.delete( s.get( Part.class, p2.getId() )); s.delete(baz); t.commit(); s.close(); }
Example 11
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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(); }
Example 12
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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(); }
Example 13
Source File: TestReader.java From LibrarySystem with Apache License 2.0 | 5 votes |
@Test public void testDeleteReader(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Reader reader = (Reader) session.get(Reader.class, 1); session.delete(reader); transaction.commit(); session.close(); }
Example 14
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testListRemove() throws Exception { Session s = openSession(); Baz b = new Baz(); List stringList = new ArrayList(); List feeList = new ArrayList(); b.setFees(feeList); b.setStringList(stringList); feeList.add( new Fee() ); feeList.add( new Fee() ); feeList.add( new Fee() ); feeList.add( new Fee() ); stringList.add("foo"); stringList.add("bar"); stringList.add("baz"); stringList.add("glarch"); s.save(b); s.flush(); stringList.remove(1); feeList.remove(1); s.flush(); s.evict(b); s.refresh(b); assertTrue( b.getFees().size()==3 ); stringList = b.getStringList(); assertTrue( stringList.size()==3 && "baz".equals( stringList.get(1) ) && "foo".equals( stringList.get(0) ) ); s.delete(b); s.delete("from Fee"); s.flush(); s.connection().commit(); s.close(); }
Example 15
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testIdBag() throws Exception { Session s = openSession(); Baz baz = new Baz(); s.save(baz); List l = new ArrayList(); List l2 = new ArrayList(); baz.setIdFooBag(l); baz.setByteBag(l2); l.add( new Foo() ); l.add( new Bar() ); byte[] bytes = "ffo".getBytes(); l2.add(bytes); l2.add( "foo".getBytes() ); s.flush(); l.add( new Foo() ); l.add( new Bar() ); l2.add( "bar".getBytes() ); s.flush(); s.delete( l.remove(3) ); bytes[1]='o'; s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, baz.getCode()); assertTrue( baz.getIdFooBag().size()==3 ); assertTrue( baz.getByteBag().size()==3 ); bytes = "foobar".getBytes(); Iterator iter = baz.getIdFooBag().iterator(); while ( iter.hasNext() ) s.delete( iter.next() ); baz.setIdFooBag(null); baz.getByteBag().add(bytes); baz.getByteBag().add(bytes); assertTrue( baz.getByteBag().size()==5 ); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, baz.getCode()); assertTrue( baz.getIdFooBag().size()==0 ); assertTrue( baz.getByteBag().size()==5 ); baz.getIdFooBag().add( new Foo() ); iter = baz.getByteBag().iterator(); iter.next(); iter.remove(); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, baz.getCode()); assertTrue( baz.getIdFooBag().size()==1 ); assertTrue( baz.getByteBag().size()==4 ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); }
Example 16
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testUpdateCollections() throws Exception { Session s = openSession(); Holder baz = new Holder(); baz.setName("123"); Foo f1 = new Foo(); Foo f2 = new Foo(); Foo f3 = new Foo(); One o = new One(); baz.setOnes( new ArrayList() ); baz.getOnes().add(o); Foo[] foos = new Foo[] { f1, null, f2 }; baz.setFooArray(foos); baz.setFoos( new HashSet() ); baz.getFoos().add(f1); s.save(f1); s.save(f2); s.save(f3); s.save(o); s.save(baz); s.flush(); s.connection().commit(); s.close(); baz.getOnes().set(0, null); baz.getOnes().add(o); baz.getFoos().add(f2); foos[0] = f3; foos[1] = f1; s = openSession(); s.saveOrUpdate(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); Holder h = (Holder) s.load(Holder.class, baz.getId()); assertTrue( h.getOnes().get(0)==null ); assertTrue( h.getOnes().get(1)!=null ); assertTrue( h.getFooArray()[0]!=null); assertTrue( h.getFooArray()[1]!=null); assertTrue( h.getFooArray()[2]!=null); assertTrue( h.getFoos().size()==2 ); s.connection().commit(); s.close(); baz.getFoos().remove(f1); baz.getFoos().remove(f2); baz.getFooArray()[0]=null; baz.getFooArray()[0]=null; baz.getFooArray()[0]=null; s = openSession(); s.saveOrUpdate(baz); s.delete("from Foo"); baz.getOnes().remove(o); s.delete("from One"); s.delete(baz); s.flush(); s.connection().commit(); s.close(); }
Example 17
Source File: MasterDetailTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testCategories() throws Exception { Session s = openSession(); Category c = new Category(); c.setName(Category.ROOT_CATEGORY); Category c1 = new Category(); Category c2 = new Category(); Category c3 = new Category(); c.getSubcategories().add(c1); c.getSubcategories().add(c2); c2.getSubcategories().add(null); c2.getSubcategories().add(c3); s.save(c); s.flush(); s.connection().commit(); s.close(); s = openSession(); Transaction tx = s.beginTransaction(); s.lock(c, LockMode.UPGRADE); Category loaded = (Category) s.load( Category.class, new Long( c3.getId() ) ); assertTrue( s.contains(c3) ); assertTrue(loaded==c3); assertTrue( s.getCurrentLockMode(c3)==LockMode.NONE ); assertTrue( s.getCurrentLockMode(c)==LockMode.UPGRADE ); s.flush(); tx.commit(); s.close(); s = openSession(); loaded = (Category) s.load( Category.class, new Long( c.getId() ) ); assertFalse( Hibernate.isInitialized( loaded.getSubcategories() ) ); s.connection().commit(); s.close(); s = openSession(); s.lock(loaded, LockMode.NONE); assertTrue( loaded.getSubcategories().size()==2 ); s.connection().commit(); s.close(); s = openSession(); c = (Category) s.load( Category.class, new Long( c.getId() ) ); System.out.println( c.getSubcategories() ); assertTrue( c.getSubcategories().get(0)!=null && c.getSubcategories().get(1)!=null ); List list = ( (Category) c.getSubcategories().get(1) ).getSubcategories(); assertTrue( list.get(1)!=null && list.get(0)==null ); assertTrue( s.iterate("from Category c where c.name = org.hibernate.test.legacy.Category.ROOT_CATEGORY").hasNext() ); s.delete(c); s.flush(); s.connection().commit(); s.close(); }
Example 18
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testMoveLazyCollection() throws Exception { Session s = openSession(); Baz baz = new Baz(); Baz baz2 = new Baz(); baz.setFooSet( new HashSet() ); Foo foo = new Foo(); baz.getFooSet().add(foo); s.save(foo); s.save(baz); s.save(baz2); foo.setBytes( "foobar".getBytes() ); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (Foo) s.get( Foo.class, foo.getKey() ); assertTrue( Hibernate.isInitialized( foo.getBytes() ) ); assertTrue( foo.getBytes().length==6 ); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertTrue( baz.getFooSet().size()==1 ); s.flush(); s.connection().commit(); s.close(); getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet"); s = openSession(); baz = (Baz) s.get( Baz.class, baz.getCode() ); assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); baz2 = (Baz) s.get( Baz.class, baz2.getCode() ); baz2.setFooSet( baz.getFooSet() ); baz.setFooSet(null); assertFalse( Hibernate.isInitialized( baz2.getFooSet() ) ); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (Foo) s.get( Foo.class, foo.getKey() ); assertTrue( foo.getBytes().length==6 ); baz = (Baz) s.get( Baz.class, baz.getCode() ); baz2 = (Baz) s.get( Baz.class, baz2.getCode() ); assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); assertTrue( baz.getFooSet().size()==0 ); assertTrue( Hibernate.isInitialized( baz2.getFooSet() ) ); //fooSet has batching enabled assertTrue( baz2.getFooSet().size()==1 ); s.delete(baz); s.delete(baz2); s.delete(foo); s.flush(); s.connection().commit(); s.close(); }
Example 19
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testAutoFlushCollections() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Baz baz = new Baz(); baz.setDefaults(); s.save(baz); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); baz = (Baz) s.load(Baz.class, baz.getCode()); baz.getStringArray()[0] = "bark"; Iterator i = s.iterate("select elements(baz.stringArray) from Baz baz"); boolean found = false; while ( i.hasNext() ) { if ( "bark".equals( i.next() ) ) found = true; } assertTrue(found); baz.setStringArray(null); i = s.iterate("select distinct elements(baz.stringArray) from Baz baz"); assertTrue( !i.hasNext() ); baz.setStringArray( new String[] { "foo", "bar" } ); i = s.iterate("select elements(baz.stringArray) from Baz baz"); assertTrue( i.hasNext() ); Foo foo = new Foo(); s.save(foo); s.flush(); baz.setFooArray( new Foo[] {foo} ); i = s.iterate("select foo from Baz baz join baz.fooArray foo"); found = false; while ( i.hasNext() ) { if ( foo==i.next() ) found = true; } assertTrue(found); baz.getFooArray()[0] = null; i = s.iterate("select foo from Baz baz join baz.fooArray foo"); assertTrue( !i.hasNext() ); baz.getFooArray()[0] = foo; i = s.iterate("select elements(baz.fooArray) from Baz baz"); assertTrue( i.hasNext() ); if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof InterbaseDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof SAPDBDialect) ) { baz.getFooArray()[0] = null; i = s.iterate( "from Baz baz where ? in elements(baz.fooArray)", foo, Hibernate.entity(Foo.class) ); assertTrue( !i.hasNext() ); baz.getFooArray()[0] = foo; i = s.iterate( "select foo from Foo foo where foo in " + "(select elt from Baz baz join baz.fooArray elt)" ); assertTrue( i.hasNext() ); } s.delete(foo); s.delete(baz); tx.commit(); s.close(); }
Example 20
Source File: IJTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testFormulaDiscriminator() throws Exception { if ( ( getDialect() instanceof OracleDialect ) || ( getDialect() instanceof HSQLDialect ) ) return; Session s = getSessions().openSession(); I i = new I(); i.setName( "i" ); i.setType( 'a' ); J j = new J(); j.setName( "j" ); j.setType( 'x' ); j.setAmount( 1.0f ); Serializable iid = s.save(i); Serializable jid = s.save(j); s.flush(); s.connection().commit(); s.close(); getSessions().evict(I.class); s = getSessions().openSession(); j = (J) s.get(I.class, jid); i = (I) s.get(I.class, iid); assertTrue( i.getClass()==I.class ); j.setAmount( 0.5f ); s.lock(i, LockMode.UPGRADE); s.flush(); s.connection().commit(); s.close(); s = getSessions().openSession(); j = (J) s.get(I.class, jid, LockMode.UPGRADE); i = (I) s.get(I.class, iid, LockMode.UPGRADE); s.flush(); s.connection().commit(); s.close(); s = getSessions().openSession(); assertTrue( s.find("from I").size()==2 ); assertTrue( s.find("from J").size()==1 ); assertTrue( s.find("from I i where i.class = 0").size()==1 ); assertTrue( s.find("from I i where i.class = 1").size()==1 ); s.connection().commit(); s.close(); s = getSessions().openSession(); j = (J) s.get(J.class, jid); i = (I) s.get(I.class, iid); s.delete(j); s.delete(i); s.flush(); s.connection().commit(); s.close(); }