org.hibernate.classic.Session#get ( )源码实例Demo

下面列出了org.hibernate.classic.Session#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: OnLineTest   文件: TestChoice.java
@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();
}
 
源代码2 项目: cacheonix-core   文件: FooBarTest.java
public void testForCertain() throws Exception {
	Glarch g = new Glarch();
	Glarch g2 = new Glarch();
	List set = new ArrayList();
	set.add("foo");
	g2.setStrings(set);
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Serializable gid = (Serializable) s.save(g);
	Serializable g2id = (Serializable) s.save(g2);
	t.commit();
	assertTrue( g.getVersion()==0 );
	assertTrue( g2.getVersion()==0 );
	s.close();

	s = openSession();
	t = s.beginTransaction();
	g = (Glarch) s.get(Glarch.class, gid);
	g2 = (Glarch) s.get(Glarch.class, g2id);
	assertTrue( g2.getStrings().size()==1 );
	s.delete(g);
	s.delete(g2);
	t.commit();
	s.close();

}
 
源代码3 项目: LibrarySystem   文件: TestBack.java
@Test
public void testGetBack(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	BackInfo back = (BackInfo) session.get(BackInfo.class, 4);
	System.out.println(back);
	session.close();
}
 
源代码4 项目: LibrarySystem   文件: TestReader.java
@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();
}
 
源代码5 项目: LibrarySystem   文件: TestBookType.java
@Test
public void testDeleteBookType(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	BookType booktype = (BookType) session.get(BookType.class, 1);
	session.delete(booktype);
	transaction.commit();
	session.close();

}
 
源代码6 项目: LibrarySystem   文件: TestAdmin.java
@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();
}
 
源代码7 项目: LibrarySystem   文件: TestBook.java
@Test
public void testDelete(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	Book book = (Book) session.get(Book.class, 1);
	session.delete(book);
	transaction.commit();
	session.close();
}
 
源代码8 项目: OnLineTest   文件: TestSubject.java
@Test
public void testGetSubject(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	Subject subject = (Subject) session.get(Subject.class, 1);
	System.out.println(subject.getChoices().size());
	Set<Choice> choices = subject.getChoices();
	for(Choice choice : choices){
		System.out.println(choice.getAnswer());
	}
	session.close();
}
 
源代码9 项目: cacheonix-core   文件: MultiRepresentationTest.java
public void testPojoRetreival() {
	TestData testData = new TestData();
	testData.create();

	Session session = openSession();
	Transaction txn = session.beginTransaction();

	Stock stock = ( Stock ) session.get( Stock.class, new Long( 1 ) );
	assertEquals( "Something wrong!", new Long( 1 ), stock.getId() );

	txn.commit();
	session.close();

	testData.destroy();
}
 
源代码10 项目: cacheonix-core   文件: FooBarTest.java
public void testWierdSession() throws Exception {
		Session s = openSession();
		Transaction t = s.beginTransaction();
		Serializable id =  s.save( new Foo() );
		t.commit();
		s.close();

		s = openSession();
		s.setFlushMode(FlushMode.NEVER);
	t = s.beginTransaction();
	Foo foo = (Foo) s.get(Foo.class, id);
	t.commit();
	s.disconnect();

	s.reconnect();
	t = s.beginTransaction();
	s.flush();
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	foo = (Foo) s.get(Foo.class, id);
	s.delete(foo);
	t.commit();
	s.close();
}
 
源代码11 项目: cacheonix-core   文件: CustomSQLTest.java
public void testJoinedSubclass() throws HibernateException, SQLException {
	Medication m = new Medication();

	m.setPrescribedDrug(new Drug());

	m.getPrescribedDrug().setName("Morphine");


	Session s = openSession();

	s.save(m.getPrescribedDrug());
	s.save(m);

	s.flush();
	s.connection().commit();
	s.close();
	s = openSession();

	Medication m2  = (Medication) s.get(Medication.class, m.getId());
	assertNotSame(m, m2);

	s.flush();
	s.connection().commit();
	s.close();

}
 
源代码12 项目: cacheonix-core   文件: SQLLoaderTest.java
public void testCompositeIdId() throws HibernateException, SQLException {
 // issue HHH-21
    Session s = openSession();

    CompositeIdId id = new CompositeIdId();
    id.setName("Max");
    id.setSystem("c64");
    id.setId("games");

    s.save(id);
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    // having a composite id with one property named id works since the map used by sqlloader to map names to properties handles it.
    Query query = s.createSQLQuery("select system as {c.system}, id as {c.id}, name as {c.name}, foo as {c.composite.foo}, bar as {c.composite.bar} from CompositeIdId where system=? and id=?", "c", CompositeIdId.class);
    query.setString(0, "c64");
    query.setString(1, "games");

    CompositeIdId id2 = (CompositeIdId) query.uniqueResult();
    check(id, id2);

    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();

    CompositeIdId useForGet = new CompositeIdId();
    useForGet.setSystem("c64");
    useForGet.setId("games");
    // this doesn't work since the verification does not take column span into respect!
    CompositeIdId getted = (CompositeIdId) s.get(CompositeIdId.class, useForGet);
    check(id,getted);


    s.connection().commit();
    s.close();

}
 
源代码13 项目: cacheonix-core   文件: FooBarTest.java
public void testDereferenceLazyCollection() throws Exception {
	Session s = openSession();
	Baz baz = new Baz();
	baz.setFooSet( new HashSet() );
	Foo foo = new Foo();
	baz.getFooSet().add(foo);
	s.save(foo);
	s.save(baz);
	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() ) );
	baz.setFooSet(null);
	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() );
	assertFalse( Hibernate.isInitialized( baz.getFooSet() ) );
	assertTrue( baz.getFooSet().size()==0 );
	s.delete(baz);
	s.delete(foo);
	s.flush();
	s.connection().commit();
	s.close();
}
 
源代码14 项目: cacheonix-core   文件: CustomSQLTest.java
public void testCRUD() throws HibernateException, SQLException {

		if ( getDialect() instanceof HSQLDialect ) return;
		if ( getDialect() instanceof MySQLDialect ) return;

		Person p = new Person();

		p.setName("Max");
		p.setLastName("Andersen");
		p.setNationalID("110974XYZ�");
		p.setAddress("P. P. Street 8");

		Session s = openSession();

		s.save(p);
		s.flush();

		s.connection().commit();
		s.close();

		getSessions().evict(Person.class);
		s = openSession();

		Person p2 = (Person) s.get(Person.class, p.getId());
		assertNotSame(p, p2);
		assertEquals(p2.getId(),p.getId());
		assertEquals(p2.getLastName(),p.getLastName());
		s.flush();

		List list = s.find("select p from Party as p");
		assertTrue(list.size() == 1);

		s.connection().commit();
		s.close();

		s = openSession();

		list = s.find("select p from Person as p where p.address = 'L�rkev�nget 1'");
		assertTrue(list.size() == 0);
		p.setAddress("L�rkev�nget 1");
		s.update(p);
		list = s.find("select p from Person as p where p.address = 'L�rkev�nget 1'");
		assertTrue(list.size() == 1);
		list = s.find("select p from Party as p where p.address = 'P. P. Street 8'");
		assertTrue(list.size() == 0);

		s.delete(p);
		list = s.find("select p from Person as p");
		assertTrue(list.size() == 0);

		s.connection().commit();
		s.close();


	}
 
源代码15 项目: cacheonix-core   文件: FooBarTest.java
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();
}
 
源代码16 项目: cacheonix-core   文件: ABCTest.java
public void testSubclassing() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	C1 c1 = new C1();
	D d = new D();
	d.setAmount(213.34f);
	c1.setAddress("foo bar");
	c1.setCount(23432);
	c1.setName("c1");
	c1.setBName("a funny name");
	c1.setD(d);
	s.save(c1);
	d.setId( c1.getId() );
	s.save(d);

	assertTrue( s.find("from C2 c where 1=1 or 1=1").size()==0 );

	t.commit();
	s.close();

	getSessions().evict(A.class);
	
	s = openSession();
	t = s.beginTransaction();
	c1 = (C1) s.get( A.class, c1.getId() );
	assertTrue(
		c1.getAddress().equals("foo bar") &&
		(c1.getCount()==23432) &&
		c1.getName().equals("c1") &&
		c1.getD().getAmount()>213.3f
	);
	assertEquals( "a funny name", c1.getBName() );
	t.commit();
	s.close();
	
	getSessions().evict(A.class);

	s = openSession();
	t = s.beginTransaction();
	c1 = (C1) s.get( B.class, c1.getId() );
	assertTrue(
		c1.getAddress().equals("foo bar") &&
		(c1.getCount()==23432) &&
		c1.getName().equals("c1") &&
		c1.getD().getAmount()>213.3f
	);
	assertEquals( "a funny name", c1.getBName() );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	c1 = (C1) s.load( C1.class, c1.getId() );
	assertTrue(
		c1.getAddress().equals("foo bar") &&
		(c1.getCount()==23432) &&
		c1.getName().equals("c1") &&
		c1.getD().getAmount()>213.3f
	);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	List bs = s.createQuery("from B").list();
	for (int i=0; i<bs.size(); i++) {
		C1 b = (C1) bs.get(i);
		s.delete(b);
		s.delete( b.getD() );
	}
	t.commit();
	s.close();
}
 
源代码17 项目: cacheonix-core   文件: IJ2Test.java
public void testUnionSubclass() throws Exception {
	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);
	j = (J) s.get(J.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();

	getSessions().evict(I.class);

	s = getSessions().openSession();
	j = (J) s.get(J.class, jid);
	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();

	getSessions().evict(I.class);

	s = getSessions().openSession();
	assertTrue( s.find("from I").size()==2 );
	assertTrue( s.find("from J").size()==1 );
	assertTrue( s.find("from J j where j.amount > 0 and j.name is not null").size()==1 );
	assertTrue( s.find("from I i where i.class = org.hibernate.test.legacy.I").size()==1 );
	assertTrue( s.find("from I i where i.class = J").size()==1 );
	s.connection().commit();
	s.close();

	getSessions().evict(I.class);

	s = getSessions().openSession();
	j = (J) s.get(J.class, jid);
	i = (I) s.get(I.class, iid);
	K k = new K();
	Serializable kid = s.save(k);
	i.setParent(k);
	j.setParent(k);
	s.flush();
	s.connection().commit();
	s.close();

	getSessions().evict(I.class);

	s = getSessions().openSession();
	j = (J) s.get(J.class, jid);
	i = (I) s.get(I.class, iid);
	k = (K) s.get(K.class, kid);
	System.out.println(k + "=" + i.getParent());
	assertTrue( i.getParent()==k );
	assertTrue( j.getParent()==k );
	assertTrue( k.getIs().size()==2 );
	s.flush();
	s.connection().commit();
	s.close();

	getSessions().evict(I.class);

	s = getSessions().openSession();
	assertTrue( s.find("from K k inner join k.is i where i.name = 'j'").size()==1 );
	assertTrue( s.find("from K k inner join k.is i where i.name = 'i'").size()==1 );
	assertTrue( s.find("from K k left join fetch k.is").size()==2 );
	s.connection().commit();
	s.close();

	s = getSessions().openSession();
	j = (J) s.get(J.class, jid);
	i = (I) s.get(I.class, iid);
	k = (K) s.get(K.class, kid);
	s.delete(k);
	s.delete(j);
	s.delete(i);
	s.flush();
	s.connection().commit();
	s.close();

}
 
源代码18 项目: cacheonix-core   文件: ParentChildTest.java
public void testLocking() throws Exception {
	Session s = openSession();
	Transaction tx = s.beginTransaction();
	Simple s1 = new Simple(); s1.setCount(1);
	Simple s2 = new Simple(); s2.setCount(2);
	Simple s3 = new Simple(); s3.setCount(3);
	Simple s4 = new Simple(); s4.setCount(4);
	s.save(s1, new Long(1) );
	s.save(s2, new Long(2) );
	s.save(s3, new Long(3) );
	s.save(s4, new Long(4) );
	assertTrue( s.getCurrentLockMode(s1)==LockMode.WRITE );
	tx.commit();
	s.close();

	s = openSession();
	tx = s.beginTransaction();
	s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.NONE);
	assertTrue( s.getCurrentLockMode(s1)==LockMode.READ || s.getCurrentLockMode(s1)==LockMode.NONE ); //depends if cache is enabled
	s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.READ);
	assertTrue( s.getCurrentLockMode(s2)==LockMode.READ );
	s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.UPGRADE);
	assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );
	s4 = (Simple) s.get(Simple.class, new Long(4), LockMode.UPGRADE_NOWAIT);
	assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );

	s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.UPGRADE); //upgrade
	assertTrue( s.getCurrentLockMode(s1)==LockMode.UPGRADE );
	s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.NONE);
	assertTrue( s.getCurrentLockMode(s2)==LockMode.READ );
	s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.READ);
	assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );
	s4 = (Simple) s.load(Simple.class, new Long(4), LockMode.UPGRADE);
	assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );

	s.lock(s2, LockMode.UPGRADE); //upgrade
	assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE );
	s.lock(s3, LockMode.UPGRADE);
	assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );
	s.lock(s1, LockMode.UPGRADE_NOWAIT);
	s.lock(s4, LockMode.NONE);
	assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );

	tx.commit();
	tx = s.beginTransaction();

	assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE );
	assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE );
	assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE );
	assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );

	s.lock(s1, LockMode.READ); //upgrade
	assertTrue( s.getCurrentLockMode(s1)==LockMode.READ );
	s.lock(s2, LockMode.UPGRADE); //upgrade
	assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE );
	s.lock(s3, LockMode.UPGRADE_NOWAIT); //upgrade
	assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE_NOWAIT );
	s.lock(s4, LockMode.NONE);
	assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );

	s4.setName("s4");
	s.flush();
	assertTrue( s.getCurrentLockMode(s4)==LockMode.WRITE );
	tx.commit();

	tx = s.beginTransaction();

	assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE );
	assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE );
	assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE );
	assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );

	s.delete(s1); s.delete(s2); s.delete(s3); s.delete(s4);
	tx.commit();
	s.close();
}
 
源代码19 项目: cacheonix-core   文件: CustomSQLTest.java
public void testInsert() throws HibernateException, SQLException {

		if ( getDialect() instanceof HSQLDialect ) return;
		if ( getDialect() instanceof MySQLDialect ) return;
		
		Role p = new Role();

		p.setName("Patient");

		Session s = openSession();

		s.save(p);
		s.flush();

		s.connection().commit();
		s.close();

		getSessions().evict(Role.class);
		s = openSession();

		Role p2 = (Role) s.get(Role.class, new Long(p.getId()));
		assertNotSame(p, p2);
		assertEquals(p2.getId(),p.getId());
		assertTrue(p2.getName().equalsIgnoreCase(p.getName()));
		s.delete(p2);
		s.flush();


		s.connection().commit();
		s.close();


	}
 
源代码20 项目: cacheonix-core   文件: CustomSQLTest.java
public void testCollectionCUD() throws HibernateException, SQLException {
	
	if ( getDialect() instanceof HSQLDialect ) return;
	if ( getDialect() instanceof MySQLDialect ) return;
	
	Role role = new Role();

	role.setName("Jim Flanders");

	Intervention iv = new Medication();
	iv.setDescription("JF medical intervention");

	role.getInterventions().add(iv);

	List sx = new ArrayList();
	sx.add("somewhere");
	sx.add("somehow");
	sx.add("whatever");
	role.setBunchOfStrings(sx);

	Session s = openSession();

	s.save(role);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();

	Role r = (Role) s.get(Role.class,new Long(role.getId()));
	assertNotSame(role,r);

	assertEquals(1,r.getInterventions().size());

	assertEquals(3, r.getBunchOfStrings().size());

	r.getBunchOfStrings().set(1, "replacement");
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();

	r = (Role) s.get(Role.class,new Long(role.getId()));
	assertNotSame(role,r);

	assertEquals(r.getBunchOfStrings().get(1),"replacement");
	assertEquals(3, r.getBunchOfStrings().size());

	r.getBunchOfStrings().set(1, "replacement");

	r.getBunchOfStrings().remove(1);
	s.flush();

	r.getBunchOfStrings().clear();
	s.flush();

	s.connection().commit();
	s.close();

}