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

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

源代码1 项目: LibrarySystem   文件: TestBookType.java
@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();
	}
 
源代码2 项目: cacheonix-core   文件: MasterDetailTest.java
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();
}
 
源代码3 项目: cacheonix-core   文件: SQLFunctionsTest.java
public void testNothinToUpdate() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();
}
 
源代码4 项目: cacheonix-core   文件: MultiTableTest.java
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();
}
 
public void testNothinToUpdate() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();
}
 
源代码6 项目: cacheonix-core   文件: FooBarTest.java
public void testReuseDeletedCollection() throws Exception {
	Session s = openSession();
	Baz baz = new Baz();
	baz.setDefaults();
	s.save(baz);
	s.flush();
	s.delete(baz);
	Baz baz2 = new Baz();
	baz2.setStringArray( new String[] {"x-y-z"} );
	s.save(baz2);
	s.flush();
	s.connection().commit();
	s.close();

	baz2.setStringSet( baz.getStringSet() );
	baz2.setStringArray( baz.getStringArray() );
	baz2.setFooArray( baz.getFooArray() );

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

	s = openSession();
	baz2 = (Baz) s.load( Baz.class, baz2.getCode() );
	assertTrue( baz2.getStringArray().length==3 );
	assertTrue( baz2.getStringSet().size()==3 );
	s.delete(baz2);
	s.flush();
	s.connection().commit();
	s.close();


}
 
源代码7 项目: cacheonix-core   文件: FooBarTest.java
public void testDeleteUpdatedTransient() throws Exception {
	Fee fee = new Fee();
	Fee fee2 = new Fee();
	fee2.setAnotherFee(fee);
	Session s = openSession();
	Transaction tx = s.beginTransaction();
	s.save(fee);
	s.save(fee2);
	s.flush();
	fee.setCount(123);
	tx.commit();
	s.close();
	s = openSession();
	tx = s.beginTransaction();
	s.update(fee);
	//fee2.setAnotherFee(null);
	s.update(fee2);
	s.delete(fee);
	s.delete(fee2);
	tx.commit();
	s.close();
	s = openSession();
	tx = s.beginTransaction();
	assertTrue( s.find("from Fee fee").size()==0 );
	tx.commit();
	s.close();
}
 
源代码8 项目: cacheonix-core   文件: FooBarTest.java
public void testUpdateOrder() throws Exception {
	Session s = openSession();
	Fee fee1 = new Fee();
	s.save(fee1);
	Fee fee2 = new Fee();
	fee1.setFee(fee2);
	fee2.setFee(fee1);
	fee2.setFees( new HashSet() );
	Fee fee3 = new Fee();
	fee3.setFee(fee1);
	fee3.setAnotherFee(fee2);
	fee2.setAnotherFee(fee3);
	s.save(fee3);
	s.save(fee2);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	fee1.setCount(10);
	fee2.setCount(20);
	fee3.setCount(30);
	s.update(fee1);
	s.update(fee2);
	s.update(fee3);
	s.flush();
	s.delete(fee1);
	s.delete(fee2);
	s.delete(fee3);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	Transaction tx = s.beginTransaction();
	assertTrue( s.find("from Fee fee").size()==0 );
	tx.commit();
	s.close();
}
 
源代码9 项目: cacheonix-core   文件: FooBarTest.java
public void testVeto() throws Exception {
	Session s = openSession();
	Vetoer v = new Vetoer();
	s.save(v); Serializable id = s.save(v);
	s.flush();
	s.connection().commit();
	s.close();
	s = openSession();
	s.update(v, id); s.update(v, id);
	s.delete(v); s.delete(v);
	s.flush();
	s.connection().commit();
	s.close();
}
 
源代码10 项目: cacheonix-core   文件: SQLFunctionsTest.java
public void testBroken() throws Exception {
	if (getDialect() instanceof Oracle9Dialect) return;
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Broken b = new Fixed();
	b.setId( new Long(123));
	b.setOtherId("foobar");
	s.save(b);
	s.flush();
	b.setTimestamp( new Date() );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update(b);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	b = (Broken) s.load( Broken.class, b );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.delete(b);
	t.commit();
	s.close();
}
 
public void testBroken() throws Exception {
	if (getDialect() instanceof Oracle9Dialect) return;
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Broken b = new Fixed();
	b.setId( new Long(123));
	b.setOtherId("foobar");
	s.save(b);
	s.flush();
	b.setTimestamp( new Date() );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update(b);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	b = (Broken) s.load( Broken.class, b );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.delete(b);
	t.commit();
	s.close();
}
 
源代码12 项目: cacheonix-core   文件: FooBarTest.java
public void testUpdate() throws Exception {
	Session s = openSession();
	Foo foo = new Foo();
	s.save(foo);
	s.flush();
	s.connection().commit();
	s.close();

	foo = (Foo) SerializationHelper.deserialize( SerializationHelper.serialize(foo) );

	s = openSession();
	FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
	foo2.setString("dirty");
	foo2.setBoolean( new Boolean(false) );
	foo2.setBytes( new byte[] { 1,2,3} );
	foo2.setDate(null);
	foo2.setShort( new Short("69") );
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	foo2.setString("dirty again");
	s.update(foo2);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	foo2.setString("dirty again 2");
	s.update(foo2);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	Foo foo3 = new Foo();
	s.load( foo3, foo.getKey() );
	// There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
	assertTrue( "update", foo2.equalsFoo(foo3) );
	s.delete(foo3);
	s.delete("from Glarch");
	s.flush();
	s.connection().commit();
	s.close();

}
 
public void testCachedQuery() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=:name");
	q.setString("name", "Simple 2");
	q.setCacheable(true);
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
源代码14 项目: cacheonix-core   文件: FooBarTest.java
public void testTransientOrphanDelete() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Baz baz = new Baz();
	Set bars = new HashSet();
	baz.setCascadingBars(bars);
	bars.add( new Bar() );
	bars.add( new Bar() );
	bars.add( new Bar() );
	List foos = new ArrayList();
	foos.add( new Foo() );
	foos.add( new Foo() );
	baz.setFooBag(foos);
	s.save(baz);
	Iterator i = new JoinedIterator( new Iterator[] {foos.iterator(), bars.iterator()} );
	while ( i.hasNext() ) {
		FooComponent cmp = ( (Foo) i.next() ).getComponent();
		s.delete( cmp.getGlarch() );
		cmp.setGlarch(null);
	}
	t.commit();
	s.close();

	bars.remove( bars.iterator().next() );
	foos.remove(1);
	s = openSession();
	t = s.beginTransaction();
	s.update(baz);
	assertEquals( 2, s.find("From Bar bar").size() );
	assertEquals( 3, s.find("From Foo foo").size() );
	t.commit();
	s.close();

	foos.remove(0);
	s = openSession();
	t = s.beginTransaction();
	s.update(baz);
	bars.remove( bars.iterator().next() );
	assertEquals( 1, s.find("From Foo foo").size() );
	s.delete(baz);
	//s.flush();
	assertEquals( 0, s.find("From Foo foo").size() );
	t.commit();
	s.close();

}
 
public void testCachedQueryRegion() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
源代码16 项目: cacheonix-core   文件: MasterDetailTest.java
public void testCollectionReplaceOnUpdate() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Category c = new Category();
	List list = new ArrayList();
	c.setSubcategories(list);
	list.add( new Category() );
	s.save(c);
	t.commit();
	s.close();
	c.setSubcategories(list);

	s = openSession();
	t = s.beginTransaction();
	s.update(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();

	assertTrue( !Hibernate.isInitialized( c.getSubcategories() ) );

	c.setSubcategories(list2);
	s = openSession();
	t = s.beginTransaction();
	s.update(c);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	c = (Category) s.load( Category.class, new Long( c.getId() ), LockMode.UPGRADE );
	assertTrue( c.getSubcategories().size()==1 );
	s.delete(c);
	t.commit();
	s.close();
}
 
源代码17 项目: cacheonix-core   文件: SQLFunctionsTest.java
public void testCachedQuery() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=:name");
	q.setString("name", "Simple 2");
	q.setCacheable(true);
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
源代码18 项目: cacheonix-core   文件: SQLFunctionsTest.java
public void testCachedQueryRegion() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
源代码19 项目: 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();


	}
 
源代码20 项目: cacheonix-core   文件: ParentChildTest.java
public void testProxyReuse() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	FooProxy foo = new Foo();
	FooProxy foo2 = new Foo();
	Serializable id = s.save(foo);
	Serializable id2 = s.save(foo2);
	foo2.setInt(1234567);
	foo.setInt(1234);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	foo = (FooProxy) s.load(Foo.class, id);
	foo2 = (FooProxy) s.load(Foo.class, id2);
	assertFalse( Hibernate.isInitialized(foo) );
	Hibernate.initialize(foo2);
	Hibernate.initialize(foo);
	assertTrue( foo.getComponent().getImportantDates().length==4 );
	assertTrue( foo2.getComponent().getImportantDates().length==4 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	foo.setFloat( new Float(1.2f) );
	foo2.setFloat( new Float(1.3f) );
	foo2.getDependent().setKey(null);
	foo2.getComponent().getSubcomponent().getFee().setKey(null);
	assertFalse( foo2.getKey().equals(id) );
	s.save(foo, "xyzid");
	s.update(foo2, id); //intentionally id, not id2!
	assertEquals( foo2.getKey(), id );
	assertTrue( foo2.getInt()==1234567 );
	assertEquals( foo.getKey(), "xyzid" );
	t.commit();
	s.close();
	
	s = openSession();
	t = s.beginTransaction();
	foo = (FooProxy) s.load(Foo.class, id);
	assertTrue( foo.getInt()==1234567 );
	assertTrue( foo.getComponent().getImportantDates().length==4 );
	String feekey = foo.getDependent().getKey();
	String fookey = foo.getKey();
	s.delete(foo);
	s.delete( s.get(Foo.class, id2) );
	s.delete( s.get(Foo.class, "xyzid") );
	assertTrue( s.delete("from java.lang.Object")==3 );
	t.commit();
	s.close();
	
	//to account for new id rollback shit
	foo.setKey(fookey);
	foo.getDependent().setKey(feekey);
	foo.getComponent().setGlarch(null);
	foo.getComponent().setSubcomponent(null);
	
	s = openSession();
	t = s.beginTransaction();
	//foo.getComponent().setGlarch(null); //no id property!
	s.replicate(foo, ReplicationMode.OVERWRITE);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Foo refoo = (Foo) s.get(Foo.class, id);
	assertEquals( feekey, refoo.getDependent().getKey() );
	s.delete(refoo);
	t.commit();
	s.close();
}