Java Code Examples for org.hibernate.classic.Session#update()

The following examples show how to use org.hibernate.classic.Session#update() . 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: MasterDetailTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 2
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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();
}
 
Example 3
Source File: MultiTableTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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();
}
 
Example 4
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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();
}
 
Example 5
Source File: TestBookType.java    From LibrarySystem with 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 6
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 7
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 8
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 9
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 10
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 11
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();


}
 
Example 12
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 13
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();

}
 
Example 14
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 15
Source File: MasterDetailTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 16
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 17
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 18
Source File: CustomSQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();


	}
 
Example 19
Source File: ParentChildTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 20
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();

}