Java Code Examples for org.hibernate.classic.Session

The following examples show how to use org.hibernate.classic.Session. These examples are extracted from open source projects. 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
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 2
Source Project: LibrarySystem   Source File: TestReader.java    License: Apache License 2.0 6 votes vote down vote up
@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
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 4
Source Project: LibrarySystem   Source File: TestBookType.java    License: Apache License 2.0 6 votes vote down vote up
@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 5
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
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 Project: OnLineTest   Source File: TestChoice.java    License: Apache License 2.0 6 votes vote down vote up
@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 Project: gocd   Source File: PipelineSqlMapDaoCachingTest.java    License: Apache License 2.0 6 votes vote down vote up
@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
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
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
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
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 13
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 14
@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 15
@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 16
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 17
@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
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 19
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 20
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 21
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 22
Source Project: lams   Source File: SpringSessionContext.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 */
@Override
public Session currentSession() throws HibernateException {
	try {
		return (org.hibernate.classic.Session) SessionFactoryUtils.doGetSession(this.sessionFactory, false);
	}
	catch (IllegalStateException ex) {
		throw new HibernateException(ex.getMessage());
	}
}
 
Example 23
Source Project: LibrarySystem   Source File: TestBack.java    License: Apache License 2.0 5 votes vote down vote up
@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 Project: LibrarySystem   Source File: TestReader.java    License: Apache License 2.0 5 votes vote down vote up
@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 25
Source Project: LibrarySystem   Source File: TestBorrowInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInfo(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	List list = session.createSQLQuery("SELECT f.borrowId,f.forfeit,f.isPay,f.aid FROM forfeitinfo  f,borrowinfo  b where  b.borrowId = f.borrowId and b.readerId =?").setString(0,"1").list();

	Object[] object = (Object[]) list.get(0);
	System.out.println(object[0]);
	
session.close();
}
 
Example 26
public void testDeleteRecursive() throws Exception {
	Session s = openSession();
	Foo x = new Foo();
	Foo y = new Foo();
	x.setFoo(y);
	y.setFoo(x);
	s.save(x);
	s.save(y);
	s.flush();
	s.delete(y);
	s.delete(x);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 27
public void testCustom() throws Exception {
	GlarchProxy g = new Glarch();
	Multiplicity m = new Multiplicity();
	m.count = 12;
	m.glarch = (Glarch) g;
	g.setMultiple(m);
	Session s = openSession();
	Serializable gid = s.save(g);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	g = (Glarch) s.find("from Glarch g where g.multiple.count=12").get(0);
	s.connection().commit();
	s.close();
	
	s = openSession();
	g = (Glarch) s.find("from Glarch g where g.multiple.glarch=g and g.multiple.count=12").get(0);
	assertTrue( g.getMultiple()!=null );
	assertEquals( g.getMultiple().count, 12 );
	assertSame(g.getMultiple().glarch, g);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	g = (GlarchProxy) s.load(Glarch.class, gid);
	assertTrue( g.getMultiple()!=null );
	assertEquals( g.getMultiple().count, 12 );
	assertSame(g.getMultiple().glarch, g);
	s.delete(g);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 28
Source Project: LibrarySystem   Source File: TestAdmin.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAdmin3(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Authorization authorization = (Authorization) session.get(Authorization.class, 1);
	System.out.println(authorization.getAdmin().getName());
	session.close();
}
 
Example 29
Source Project: LibrarySystem   Source File: TestAdmin.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSaveAdmin3(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	Authorization authorization = new Authorization();
	
	authorization.setAid(2);
	session.save(authorization);
	
	transaction.commit();
	session.close();
}
 
Example 30
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();

}