org.hibernate.classic.Session Java Examples
The following examples show how to use
org.hibernate.classic.Session.
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: FumTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testCompositeIDOneToOne() throws Exception { Session s = openSession(); Transaction txn = s.beginTransaction(); Fum fum = new Fum( fumKey("fum") ); fum.setFum("fee fi fo"); //s.save(fum); Fumm fumm = new Fumm(); fumm.setFum(fum); s.save(fumm); txn.commit(); s.close(); s = openSession(); txn = s.beginTransaction(); fumm = (Fumm) s.load( Fumm.class, fumKey("fum") ); //s.delete( fumm.getFum() ); s.delete(fumm); txn.commit(); s.close(); }
Example #2
Source File: TestReader.java From LibrarySystem with Apache License 2.0 | 6 votes |
@Test public void testSaveReader2(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Reader reader = new Reader(); reader.setName("菜肉"); reader.setPwd("123"); ReaderType readerType = new ReaderType(); readerType.setReaderTypeId(1); reader.setReaderType(readerType); session.save(reader); transaction.commit(); session.close(); }
Example #3
Source File: TestBookType.java From LibrarySystem with Apache License 2.0 | 6 votes |
@Test public void testUpdateBook(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); String hql= "from BookType"; List createQuery = session.createQuery(hql).list(); BookType bookType = (BookType) createQuery.get(0); System.out.println(bookType); // Set<Book> books = bookType.getBooks(); // for(Book book : books){ // book.setState(0); // } session.update(bookType); transaction.commit(); session.close(); }
Example #4
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testLateCollectionAdd() throws Exception { Session s = openSession(); Baz baz = new Baz(); List l = new ArrayList(); baz.setStringList(l); l.add("foo"); Serializable id = s.save(baz); l.add("bar"); s.flush(); l.add("baz"); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, id); assertTrue( baz.getStringList().size()==3 && baz.getStringList().contains("bar") ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); }
Example #5
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testManyToManyBag() throws Exception { Session s = openSession(); Baz baz = new Baz(); Serializable id = s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, id); baz.getFooBag().add( new Foo() ); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load(Baz.class, id); assertTrue( !Hibernate.isInitialized( baz.getFooBag() ) ); assertTrue( baz.getFooBag().size()==1 ); if ( !(getDialect() instanceof HSQLDialect) ) assertTrue( Hibernate.isInitialized( baz.getFooBag().iterator().next() ) ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); }
Example #6
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testRemoveContains() throws Exception { Session s = openSession(); Baz baz = new Baz(); baz.setDefaults(); s.save(baz); s.flush(); assertTrue( s.contains(baz) ); s.evict(baz); assertFalse( s.contains(baz) ); Baz baz2 = (Baz) s.load( Baz.class, baz.getCode() ); assertFalse(baz==baz2); s.delete(baz2); s.flush(); s.connection().commit(); s.close(); }
Example #7
Source File: TestChoice.java From OnLineTest with Apache License 2.0 | 6 votes |
@Test public void testSaveChoice(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Subject subject = (Subject) session.get(Subject.class, 1); Choice choice = new Choice(); choice.setAnswer("A"); choice.setQuestion("下列选项正确的是:"); choice.setOptionA("选择A"); choice.setOptionB("选择B"); choice.setOptionC("选择C"); choice.setOptionD("选择D"); choice.setSubject(subject); session.save(choice); transaction.commit(); session.close(); }
Example #8
Source File: PipelineSqlMapDaoCachingTest.java From gocd with Apache License 2.0 | 6 votes |
@Before public void setup() throws Exception { transactionSynchronizationManager = mock(TransactionSynchronizationManager.class); goCache = new StubGoCache(new TestTransactionSynchronizationManager()); goCache.clear(); mockTemplate = mock(SqlMapClientTemplate.class); repository = mock(MaterialRepository.class); environmentVariableDao = mock(EnvironmentVariableDao.class); SessionFactory mockSessionFactory = mock(SessionFactory.class); repository = mock(MaterialRepository.class); transactionTemplate = mock(TransactionTemplate.class); GoConfigDao configFileDao = mock(GoConfigDao.class); timeProvider = mock(TimeProvider.class); pipelineDao = new PipelineSqlMapDao(null, repository, goCache, environmentVariableDao, transactionTemplate, null, transactionSynchronizationManager, null, configFileDao, mock(Database.class), timeProvider); pipelineDao.setSqlMapClientTemplate(mockTemplate); Session session = mock(Session.class); when(mockSessionFactory.getCurrentSession()).thenReturn(session); when(configFileDao.load()).thenReturn(GoConfigMother.defaultCruiseConfig()); }
Example #9
Source File: MasterDetailTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testPolymorphicCriteria() throws Exception { Session s = openSession(); Transaction txn = s.beginTransaction(); Category f = new Category(); Single b = new Single(); b.setId("asdfa"); b.setString("asdfasdf"); s.save(f); s.save(b); List list = s.createCriteria(Object.class).list(); assertTrue( list.size()==2 ); assertTrue( list.contains(f) && list.contains(b) ); s.delete(f); s.delete(b); txn.commit(); s.close(); }
Example #10
Source File: FumTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
private Session spoofSerialization(Session session) throws IOException { try { // Serialize the incoming out to memory ByteArrayOutputStream serBaOut = new ByteArrayOutputStream(); ObjectOutputStream serOut = new ObjectOutputStream(serBaOut); serOut.writeObject(session); // Now, re-constitute the model from memory ByteArrayInputStream serBaIn = new ByteArrayInputStream(serBaOut.toByteArray()); ObjectInputStream serIn = new ObjectInputStream(serBaIn); Session outgoing = (Session) serIn.readObject(); return outgoing; } catch (ClassNotFoundException cnfe) { throw new IOException("Unable to locate class on reconstruction"); } }
Example #11
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testObjectType() throws Exception { Session s = openSession(); GlarchProxy g = new Glarch(); Foo foo = new Foo(); g.setAny(foo); Serializable gid = s.save(g); s.save(foo); s.flush(); s.connection().commit(); s.close(); s = openSession(); g = (GlarchProxy) s.load(Glarch.class, gid); assertTrue( g.getAny()!=null && g.getAny() instanceof FooProxy ); s.delete( g.getAny() ); s.delete(g); //s.delete( g.getAny() ); s.flush(); s.connection().commit(); s.close(); }
Example #12
Source File: MultiTableTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testJoins() throws Exception { Session s = openSession(); s.find("from Lower l join l.yetanother l2 where lower(l2.name) > 'a'"); s.find("from Lower l where lower(l.yetanother.top.name) > 'a'"); s.find("from SubMulti sm join sm.children smc where smc.name > 'a'"); s.find("select s, ya from Lower s join s.yetanother ya"); s.find("from Lower s1 join s1.bag s2"); s.find("from Lower s1 left join s1.bag s2"); s.find("select s, a from Lower s join s.another a"); s.find("select s, a from Lower s left join s.another a"); s.find("from Top s, Lower ls"); s.find("from Lower ls join ls.set s where s.name > 'a'"); s.find("from Po po join po.list sm where sm.name > 'a'"); s.find("from Lower ls inner join ls.another s where s.name is not null"); s.find("from Lower ls where ls.other.another.name is not null"); s.find("from Multi m where m.derived like 'F%'"); s.find("from SubMulti m where m.derived like 'F%'"); s.connection().commit(); s.close(); }
Example #13
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testCollectionCache() throws Exception { Session s = openSession(); Baz baz = new Baz(); baz.setDefaults(); s.save(baz); s.flush(); s.connection().commit(); s.close(); s = openSession(); s.load( Baz.class, baz.getCode() ); s.flush(); s.connection().commit(); s.close(); s = openSession(); baz = (Baz) s.load( Baz.class, baz.getCode() ); s.delete(baz); s.flush(); s.connection().commit(); s.close(); }
Example #14
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testSerializableType() throws Exception { Session s = openSession(); Vetoer v = new Vetoer(); v.setStrings( new String[] { "foo", "bar", "baz" } ); s.save(v); Serializable id = s.save(v); v.getStrings()[1] = "osama"; s.flush(); s.connection().commit(); s.close(); s = openSession(); v = (Vetoer) s.load(Vetoer.class, id); assertTrue( "serializable type", v.getStrings()[1].equals("osama") ); s.delete(v); s.delete(v); s.flush(); s.connection().commit(); s.close(); }
Example #15
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testFindLoad() throws Exception { Session s = openSession(); FooProxy foo = new Foo(); s.save(foo); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo = (FooProxy) s.find("from Foo foo").get(0); FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() ); assertTrue("find returns same object as load", foo==foo2); s.flush(); s.connection().commit(); s.close(); s = openSession(); foo2 = (FooProxy) s.load( Foo.class, foo.getKey() ); foo = (FooProxy) s.find("from Foo foo").get(0); assertTrue("find returns same object as load", foo==foo2); s.delete("from Foo foo"); s.flush(); s.connection().commit(); s.close(); }
Example #16
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 #17
Source File: OpenSessionInViewTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws Exception { SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf); interceptor.setFlushMode(HibernateAccessor.FLUSH_AUTO); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); interceptor.preHandle(this.webRequest); assertTrue(TransactionSynchronizationManager.hasResource(sf)); interceptor.postHandle(this.webRequest, null); assertTrue(TransactionSynchronizationManager.hasResource(sf)); interceptor.afterCompletion(this.webRequest, null); assertFalse(TransactionSynchronizationManager.hasResource(sf)); verify(session).flush(); verify(session).close(); }
Example #18
Source File: BulkManipulationTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); }
Example #19
Source File: HibernateTemplateTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testExecuteWithNotAllowCreate() { reset(sessionFactory); given(sessionFactory.getCurrentSession()).willThrow(new HibernateException("")); HibernateTemplate ht = new HibernateTemplate(); ht.setSessionFactory(sessionFactory); ht.setAllowCreate(false); try { ht.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(org.hibernate.Session session) throws HibernateException { return null; } }); fail("Should have thrown DataAccessException"); } catch (DataAccessResourceFailureException ex) { // expected } }
Example #20
Source File: HibernateTemplateTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testExecuteWithNewSession() throws HibernateException { assertTrue("Correct allowCreate default", hibernateTemplate.isAllowCreate()); assertTrue("Correct flushMode default", hibernateTemplate.getFlushMode() == HibernateTemplate.FLUSH_AUTO); final List l = new ArrayList(); l.add("test"); List result = hibernateTemplate.executeFind(new HibernateCallback<Object>() { @Override public Object doInHibernate(org.hibernate.Session session) throws HibernateException { return l; } }); assertTrue("Correct result list", result == l); verify(session).flush(); verify(session).close(); }
Example #21
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 #22
Source File: HibernateTemplateTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testExecuteWithFetchSizeAndMaxResults() throws HibernateException { Query query1 = mock(Query.class); Query query2 = mock(Query.class); Criteria criteria = mock(Criteria.class); given(session.createQuery("some query")).willReturn(query1); given(query1.setFetchSize(10)).willReturn(query1); given(query1.setMaxResults(20)).willReturn(query1); given(session.getNamedQuery("some query name")).willReturn(query2); given(query2.setFetchSize(10)).willReturn(query2); given(query2.setMaxResults(20)).willReturn(query2); given(session.createCriteria(TestBean.class)).willReturn(criteria); given(criteria.setFetchSize(10)).willReturn(criteria); given(criteria.setMaxResults(20)).willReturn(criteria); hibernateTemplate.setFetchSize(10); hibernateTemplate.setMaxResults(20); hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(org.hibernate.Session sess) throws HibernateException { sess.createQuery("some query"); sess.getNamedQuery("some query name"); sess.createCriteria(TestBean.class); return null; } }); verify(session).flush(); verify(session).close(); }
Example #23
Source File: TestBack.java From LibrarySystem with Apache License 2.0 | 5 votes |
@Test public void testGetBack2(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); List list = session.createSQLQuery("select ba.borrowId from BackInfo ba ,BorrowInfo bo,Book bk,Reader r " +"where ba.borrowId=bo.borrowId and Bk.bookId=Bo.bookId and bo.readerId=r.readerId " +"and bk.ISBN like '%1%' and r.paperNO like '%1%'").list(); Object objects = list.get(0); System.out.println(objects); session.close(); }
Example #24
Source File: MasterDetailTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testCollectionReplace2() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Category c = new Category(); List list = new ArrayList(); c.setSubcategories(list); list.add( new Category() ); Category c2 = new Category(); s.save(c2); s.save(c); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c = (Category) s.load( Category.class, new Long( c.getId() ), LockMode.UPGRADE ); List list2 = c.getSubcategories(); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c2 = (Category) s.load( Category.class, new Long( c2.getId() ), LockMode.UPGRADE ); c2.setSubcategories(list2); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c2 = (Category) s.load( Category.class, new Long( c2.getId() ), LockMode.UPGRADE ); assertTrue( c2.getSubcategories().size()==1 ); s.delete(c2); s.delete( s.load( Category.class, new Long( c.getId() ) ) ); t.commit(); s.close(); }
Example #25
Source File: TestReader.java From LibrarySystem with Apache License 2.0 | 5 votes |
@Test public void getReader(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); String hql= "from Reader r where r.readerId=123 and r.state=1"; List createQuery = session.createQuery(hql).list(); Reader reader = (Reader) createQuery.get(0); System.out.println(reader); }
Example #26
Source File: HibernateTemplateTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testExecuteWithCacheQueriesAndCacheRegion() throws HibernateException { Query query1 = mock(Query.class); Query query2 = mock(Query.class); Criteria criteria = mock(Criteria.class); given(session.createQuery("some query")).willReturn(query1); given(query1.setCacheable(true)).willReturn(query1); given(query1.setCacheRegion("myRegion")).willReturn(query1); given(session.getNamedQuery("some query name")).willReturn(query2); given(query2.setCacheable(true)).willReturn(query2); given(query2.setCacheRegion("myRegion")).willReturn(query2); given(session.createCriteria(TestBean.class)).willReturn(criteria); given(criteria.setCacheable(true)).willReturn(criteria); given(criteria.setCacheRegion("myRegion")).willReturn(criteria); hibernateTemplate.setCacheQueries(true); hibernateTemplate.setQueryCacheRegion("myRegion"); hibernateTemplate.execute(new HibernateCallback<Object>() { @Override public Object doInHibernate(org.hibernate.Session sess) throws HibernateException { assertNotSame(session, sess); assertTrue(Proxy.isProxyClass(sess.getClass())); sess.createQuery("some query"); sess.getNamedQuery("some query name"); sess.createCriteria(TestBean.class); // should be ignored sess.close(); return null; } }); verify(session).flush(); verify(session).close(); }
Example #27
Source File: FooBarTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testRefreshProxy() throws Exception { Session s = openSession(); Glarch g = new Glarch(); Serializable gid = s.save(g); s.flush(); s.clear(); GlarchProxy gp = (GlarchProxy) s.load(Glarch.class, gid); gp.getName(); //force init s.refresh(gp); s.delete(gp); s.flush(); s.connection().commit(); s.close(); }
Example #28
Source File: SQLFunctionsTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testSetPropertiesMap() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save(simple, new Long(10) ); Map parameters = new HashMap(); parameters.put("name", simple.getName()); parameters.put("count", new Integer(simple.getCount())); Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count"); q.setProperties(((Map)parameters)); assertTrue( q.list().get(0)==simple ); List l = new ArrayList(); l.add("Simple 1"); l.add("Slimeball"); parameters.put("several", l); q = s.createQuery("from Simple s where s.name in (:several)"); q.setProperties(parameters); assertTrue( q.list().get(0)==simple ); parameters.put("stuff", l.toArray(new String[0])); q = s.createQuery("from Simple s where s.name in (:stuff)"); q.setProperties(parameters); assertTrue( q.list().get(0)==simple ); s.delete(simple); t.commit(); s.close(); }
Example #29
Source File: OpenSessionInViewTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception { SessionFactory sf = mock(SessionFactory.class); Session session = mock(Session.class); OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(sf); given(sf.openSession()).willReturn(session); given(session.getSessionFactory()).willReturn(sf); given(session.getSessionFactory()).willReturn(sf); given(session.isOpen()).willReturn(true); interceptor.preHandle(this.webRequest); assertTrue(TransactionSynchronizationManager.hasResource(sf)); // check that further invocations simply participate interceptor.preHandle(this.webRequest); assertEquals(session, SessionFactoryUtils.getSession(sf, false)); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.preHandle(this.webRequest); interceptor.postHandle(this.webRequest, null); interceptor.afterCompletion(this.webRequest, null); interceptor.postHandle(this.webRequest, null); assertTrue(TransactionSynchronizationManager.hasResource(sf)); interceptor.afterCompletion(this.webRequest, null); assertFalse(TransactionSynchronizationManager.hasResource(sf)); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }
Example #30
Source File: HibernateTemplateTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testExecuteWithNewSessionAndFlushNever() throws HibernateException { hibernateTemplate.setFlushMode(HibernateTemplate.FLUSH_NEVER); final List l = new ArrayList(); l.add("test"); List result = hibernateTemplate.executeFind(new HibernateCallback<Object>() { @Override public Object doInHibernate(org.hibernate.Session session) throws HibernateException { return l; } }); assertTrue("Correct result list", result == l); verify(session).setFlushMode(FlushMode.MANUAL); verify(session).close(); }