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

The following examples show how to use org.hibernate.classic.Session#saveOrUpdate() . 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 testOuterJoin() throws Exception {
	Session s = openSession();
	Eye e = new Eye();
	e.setName("Eye Eye");
	Jay jay = new Jay(e);
	e.setJay(jay);
	s.saveOrUpdate(e);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	e = (Eye) s.createCriteria(Eye.class).uniqueResult();
	assertTrue( Hibernate.isInitialized( e.getJay() ) );
	assertTrue( Hibernate.isInitialized( e.getJays() ) );
	s.connection().commit();
	s.close();

	s = openSession();
	jay = (Jay) s.createQuery("select new Jay(eye) from Eye eye").uniqueResult();
	assertTrue( "Eye Eye".equals( jay.getEye().getName() ) );
	s.delete( jay.getEye() );
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 2
Source File: QueryByExampleTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void initData() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", "ORM tool", "ORM tool1");
    s.saveOrUpdate(master);
    master = getMaster("hibernate", "open source", "open source1");
    s.saveOrUpdate(master);
    master = getMaster("hibernate", null, null);
    s.saveOrUpdate(master);
    t.commit();
    s.close();
}
 
Example 3
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 4
Source File: ParentChildTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCircularCascade() throws Exception {
	Session s = openSession();
	Transaction tx = s.beginTransaction();
	Circular c = new Circular();
	c.setClazz(Circular.class);
	c.setOther( new Circular() );
	c.getOther().setOther( new Circular() );
	c.getOther().getOther().setOther(c);
	c.setAnyEntity( c.getOther() );
	String id = (String) s.save(c);
	tx.commit();
	s.close();
	s = openSession();
	tx = s.beginTransaction();
	c = (Circular) s.load(Circular.class, id);
	c.getOther().getOther().setClazz(Foo.class);
	tx.commit();
	s.close();
	c.getOther().setClazz(Qux.class);
	s = openSession();
	tx = s.beginTransaction();
	s.saveOrUpdate(c);
	tx.commit();
	s.close();
	c.getOther().getOther().setClazz(Bar.class);
	s = openSession();
	tx = s.beginTransaction();
	s.saveOrUpdate(c);
	tx.commit();
	s.close();
	s = openSession();
	tx = s.beginTransaction();
	c = (Circular) s.load(Circular.class, id);
	assertTrue( c.getOther().getOther().getClazz()==Bar.class);
	assertTrue( c.getOther().getClazz()==Qux.class);
	assertTrue( c.getOther().getOther().getOther()==c);
	assertTrue( c.getAnyEntity()==c.getOther() );
	assertTrue( s.delete("from Universe")==3 );
	tx.commit();
	s.close();
}