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

The following examples show how to use org.hibernate.classic.Session#delete() . 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: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testRefresh() throws Exception {
	Session s = openSession();
	Foo foo = new Foo();
	s.save(foo);
	s.flush();
	s.connection().createStatement().executeUpdate("update "+getDialect().openQuote()+"foos"+getDialect().closeQuote()+" set long_ = -3");
	s.refresh(foo);
	assertTrue( foo.getLong().longValue()==-3l );
	assertTrue( s.getCurrentLockMode(foo)==LockMode.READ );
	s.refresh(foo, LockMode.UPGRADE);
	if ( getDialect().supportsOuterJoinForUpdate() ) {
		assertTrue( s.getCurrentLockMode(foo)==LockMode.UPGRADE );
	}
	s.delete(foo);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 2
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 3
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testNonlazyCollection() throws Exception {
	Session s = openSession();
	Baz baz = new Baz();
	s.save(baz);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	baz = (Baz) s.createCriteria(Baz.class)
		//.setComment("criteria test")
		.setFetchMode("stringDateMap", FetchMode.EAGER)
		.uniqueResult();
	assertTrue( Hibernate.isInitialized( baz.getFooToGlarch() ) );
	assertTrue( Hibernate.isInitialized( baz.getFooComponentToFoo() ) );
	assertTrue( !Hibernate.isInitialized( baz.getStringSet() ) );
	assertTrue( Hibernate.isInitialized( baz.getStringDateMap() ) );
	s.delete(baz);
	s.flush();
	s.connection().commit();
	s.close();

}
 
Example 4
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 5
Source File: SQLLoaderTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testReturnPropertyComponentRenameFailureExpected() throws HibernateException, SQLException {
	// failure expected because this was a regression introduced previously which needs to get tracked down.
	Session session = openSession();
	Componentizable componentizable = setupComponentData(session);
	
	Query namedQuery = session.getNamedQuery("queryComponentWithOtherColumn");
	List list = namedQuery.list();
	
	assertEquals(1, list.size());
	assertEquals( "flakky comp", ( (Componentizable) list.get(0) ).getComponent().getName() );
	
	session.clear();
	
	session.delete(componentizable);
	session.flush();
	
	session.connection().commit();
	session.close();
}
 
Example 6
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 7
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testOnCascadeDelete() throws Exception {

		if ( ! supportsCircularCascadeDelete() ) {
			return;
		}

		Session s = openSession();
		Baz baz = new Baz();
		baz.subs = new ArrayList();
		Baz sub = new Baz();
		sub.superBaz = baz;
		baz.subs.add(sub);
		s.save(baz);
		s.flush();
		assertTrue( s.createQuery("from Baz").list().size()==2 );
		s.connection().commit();
		s.delete(baz);
		s.flush();
		s.connection().commit();
		assertTrue( s.createQuery("from Baz").list().size()==0 );
		s.connection().commit();
		s.close();
	}
 
Example 8
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testCreate() throws Exception {
	Session s = openSession();
	Foo foo = new Foo();
	s.save(foo);
	s.flush();
	s.connection().commit();
	s.close();
	s = openSession();
	Foo foo2 = new Foo();
	s.load( foo2, foo.getKey() );
	// There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
	assertTrue( "create", foo.equalsFoo(foo2) );
	s.delete(foo2);
	s.flush();
	s.connection().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 testFetchInitializedCollection() throws Exception {
	Session s = openSession();
	Baz baz = new Baz();
	Collection fooBag = new ArrayList();
	fooBag.add( new Foo() );
	fooBag.add( new Foo() );
	baz.setFooBag(fooBag);
	s.save(baz);
	s.flush();
	fooBag = baz.getFooBag();
	s.find("from Baz baz left join fetch baz.fooBag");
	assertTrue( fooBag==baz.getFooBag() );
	s.connection().commit();
	s.close();

	s = openSession();
	baz = (Baz) s.load( Baz.class, baz.getCode() );
	Object bag = baz.getFooBag();
	assertFalse( Hibernate.isInitialized(bag) );
	s.find("from Baz baz left join fetch baz.fooBag");
	assertTrue( bag==baz.getFooBag() );
	assertTrue( baz.getFooBag().size()==2 );
	s.delete(baz);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 10
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetProperties() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(simple);
	assertTrue( q.list().get(0)==simple );
	//misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;)
	Single single = new Single() { // trivial hack to test properties with arrays.
		String[] getStuff() { return (String[]) getSeveral().toArray(new String[getSeveral().size()]); }
	};

	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	single.setSeveral(l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );


	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 11
Source File: TypeParameterTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void deleteData() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	s.delete("from Widget");
	t.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 testUpdateCollections() throws Exception {
	Session s = openSession();
	Holder baz = new Holder();
	baz.setName("123");
	Foo f1 = new Foo();
	Foo f2 = new Foo();
	Foo f3 = new Foo();
	One o = new One();
	baz.setOnes( new ArrayList() );
	baz.getOnes().add(o);
	Foo[] foos = new Foo[] { f1, null, f2 };
	baz.setFooArray(foos);
	baz.setFoos( new HashSet() );
	baz.getFoos().add(f1);
	s.save(f1);
	s.save(f2);
	s.save(f3);
	s.save(o);
	s.save(baz);
	s.flush();
	s.connection().commit();
	s.close();

	baz.getOnes().set(0, null);
	baz.getOnes().add(o);
	baz.getFoos().add(f2);
	foos[0] = f3;
	foos[1] = f1;

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

	s = openSession();
	Holder h = (Holder) s.load(Holder.class, baz.getId());
	assertTrue( h.getOnes().get(0)==null );
	assertTrue( h.getOnes().get(1)!=null );
	assertTrue( h.getFooArray()[0]!=null);
	assertTrue( h.getFooArray()[1]!=null);
	assertTrue( h.getFooArray()[2]!=null);
	assertTrue( h.getFoos().size()==2 );
	s.connection().commit();
	s.close();

	baz.getFoos().remove(f1);
	baz.getFoos().remove(f2);
	baz.getFooArray()[0]=null;
	baz.getFooArray()[0]=null;
	baz.getFooArray()[0]=null;
	s = openSession();
	s.saveOrUpdate(baz);
	s.delete("from Foo");
	baz.getOnes().remove(o);
	s.delete("from One");
	s.delete(baz);
	s.flush();
	s.connection().commit();
	s.close();

}
 
Example 14
Source File: MultiTableTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSubclassCollection() throws Exception {
	//if ( getDialect() instanceof HSQLDialect ) return; //TODO: figure out why!?
	Session s = openSession();
	SubMulti sm = new SubMulti();
	SubMulti sm1 = new SubMulti();
	SubMulti sm2 = new SubMulti();
	ArrayList list = new ArrayList();
	ArrayList anotherList = new ArrayList();
	sm.setChildren(list);
	sm.setMoreChildren(anotherList);
	sm.setExtraProp("foo");
	list.add(sm1);
	list.add(sm2);
	anotherList.add(sm1);
	anotherList.add(sm2);
	sm1.setParent(sm);
	sm2.setParent(sm);
	Serializable id = s.save(sm);
	s.save(sm1);
	s.save(sm2);
	s.flush();
	s.connection().commit();
	s.close();

	getSessions().evict(SubMulti.class);

	s = openSession();
	s.connection().createStatement().executeQuery(
		"select * from leafsubsubclass sm, nonleafsubclass m, rootclass s where sm.sid=m.sid and sm.sid=s.id1_ and sm.sid=1"
	).next();
	assertTrue( s.find("select s from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==2 );
	s.find("select c from SubMulti sm join sm.children c");
	assertTrue( s.find("select elements(sm.children) from SubMulti as sm").size()==2 );
	assertTrue( s.find("select distinct sm from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==1 );
	sm = (SubMulti) s.load(SubMulti.class, id);
	assertTrue( sm.getChildren().size()==2 );
	assertEquals(
		s.filter( sm.getMoreChildren(), "select count(*) where this.amount>-1 and this.name is null" ).iterator().next(),
		new Long(2)
	);
	assertEquals( "FOO", sm.getDerived() );
	assertSame(
		s.iterate("select distinct s from SubMulti s where s.moreChildren[1].amount < 1.0").next(),
		sm
	);
	assertTrue( sm.getMoreChildren().size()==2 );
	s.delete(sm);
	Iterator iter = sm.getChildren().iterator();
	while ( iter.hasNext() ) s.delete( iter.next() );
	s.flush();
	s.connection().commit();
	s.close();

}
 
Example 15
Source File: ParentChildTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 16
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 17
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}
 
Example 18
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 19
Source File: FumTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testBeanResultTransformer() throws HibernateException, SQLException {
	
	Session s = openSession();
	Transaction transaction = s.beginTransaction();
	Fum fum = new Fum( fumKey("fum") );
	fum.setFo( new Fum( fumKey("fo") ) );
	fum.setFum("fo fee fi");
	fum.getFo().setFum("stuff");
	Fum fr = new Fum( fumKey("fr") );
	fr.setFum("goo");
	Fum fr2 = new Fum( fumKey("fr2") );
	fr2.setFum("soo");
	fum.setFriends( new HashSet() );
	fum.getFriends().add(fr);
	fum.getFriends().add(fr2);
	s.save(fr);
	s.save(fr2);
	s.save( fum.getFo() );
	s.save(fum);
	
	Criteria test = s.createCriteria(Fum.class, "xam")
		.createCriteria("fo", "fo")
		.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
	
	Map fc = (Map) test.list().get(0);
	assertNotNull(fc.get("xam"));
	
	Criteria base = s.createCriteria(Fum.class, "fum")
	.add( Expression.like("fum", "f%") )
	.setResultTransformer(Transformers.aliasToBean(ABean.class))
	.setFetchMode("friends", FetchMode.JOIN);
	base.createCriteria("fo", "fo")
	.add( Expression.eq( "fum", fum.getFo().getFum() ) );
	ABean map = (ABean) base.list().get(0);

	assertTrue(
			map.getFum()==fum &&
			map.getFo()==fum.getFo() );
	
	s.delete(fr);
	s.delete(fr2);
	s.delete(fum);
	s.delete(fum.getFo());
	s.flush();
	transaction.commit();
	s.close();
	
}
 
Example 20
Source File: FumTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCompositeID() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	Fum fum = new Fum( fumKey("fum") );
	fum.setFum("fee fi fo");
	s.save(fum);
	assertTrue( "load by composite key", fum==s.load( Fum.class, fumKey("fum") ) );
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	fum = (Fum) s.load( Fum.class, fumKey("fum"), LockMode.UPGRADE );
	assertTrue( "load by composite key", fum!=null );

	Fum fum2 = new Fum( fumKey("fi") );
	fum2.setFum("fee fo fi");
	fum.setFo(fum2);
	s.save(fum2);
	assertTrue(
		"find composite keyed objects",
		s.find("from Fum fum where not fum.fum='FRIEND'").size()==2
	);
	assertTrue(
		"find composite keyed object",
		s.find("select fum from Fum fum where fum.fum='fee fi fo'").get(0)==fum
	);
	fum.setFo(null);
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	Iterator iter = s.iterate("from Fum fum where not fum.fum='FRIEND'");
	int i = 0;
	while ( iter.hasNext() ) {
		fum = (Fum) iter.next();
		//iter.remove();
		s.delete(fum);
		i++;
	}
	assertTrue( "iterate on composite key", i==2 );
	txn.commit();
	s.close();
}