下面列出了org.hibernate.classic.Session#save ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testNoForeignKeyViolations() throws Exception {
Session s = openSession();
Glarch g1 = new Glarch();
Glarch g2 = new Glarch();
g1.setNext(g2);
g2.setNext(g1);
s.save(g1);
s.save(g2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
List l = s.find("from Glarch g where g.next is not null");
s.delete( l.get(0) );
s.delete( l.get(1) );
s.flush();
s.connection().commit();
s.close();
}
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();
}
public void testPersistentLifecycle() throws Exception {
Session s = openSession();
Qux q = new Qux();
s.save(q);
q.setStuff("foo bar baz qux");
s.flush();
s.connection().commit();
s.close();
s = openSession();
q = (Qux) s.load( Qux.class, q.getKey() );
assertTrue( "lifecycle create", q.getCreated() );
assertTrue( "lifecycle load", q.getLoaded() );
assertTrue( "lifecycle subobject", q.getFoo()!=null );
s.delete(q);
assertTrue( "lifecycle delete", q.getDeleted() );
s.flush();
s.connection().commit();
s.close();
s = openSession();
assertTrue( "subdeletion", s.find("from Foo foo").size()==0);
s.flush();
s.connection().commit();
s.close();
}
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();
}
public void testSaveFlush() throws Exception {
Session s = openSession();
Fee fee = new Fee();
s.save( fee, "key" );
fee.setFi("blah");
s.flush();
s.connection().commit();
s.close();
s = openSession();
fee = (Fee) s.load( Fee.class, fee.getKey() );
assertTrue( "blah".equals( fee.getFi() ) );
assertTrue( "key".equals( fee.getKey() ) );
s.delete(fee);
s.flush();
s.connection().commit();
s.close();
}
public void testForeignKeys() throws Exception {
Session s = openSession();
Baz baz = new Baz();
Foo foo = new Foo();
List bag = new ArrayList();
bag.add(foo);
baz.setIdFooBag(bag);
baz.setFoo(foo);
s.save(baz);
s.flush();
s.connection().commit();
s.close();
s = openSession();
baz = (Baz) s.load( Baz.class, baz.getCode() );
s.delete(baz);
s.flush();
s.connection().commit();
s.close();
}
public void testObjectType() throws Exception {
Session s = openSession();
GlarchProxy g = new Glarch();
Foo foo = new Foo();
g.setAny(foo);
Serializable gid = s.save(g);
s.save(foo);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, gid);
assertTrue( g.getAny()!=null && g.getAny() instanceof FooProxy );
s.delete( g.getAny() );
s.delete(g);
//s.delete( g.getAny() );
s.flush();
s.connection().commit();
s.close();
}
@Test
public void testSaveAdmin(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Admin admin = new Admin();
admin.setUsername("admin");
admin.setPassword(Md5Utils.md5("admin"));
System.out.println(Md5Utils.md5("cairou"));
session.save(admin);
transaction.commit();
session.close();
}
public void testIncomingOutgoing() throws Exception {
Session s = openSession();
Master master1 = new Master();
Master master2 = new Master();
Master master3 = new Master();
s.save(master1);
s.save(master2);
s.save(master3);
master1.addIncoming(master2);
master2.addOutgoing(master1);
master1.addIncoming(master3);
master3.addOutgoing(master1);
Serializable m1id = s.getIdentifier(master1);
assertTrue( s.filter( master1.getIncoming(), "where this.id > 0 and this.name is not null").size()==2 );
s.flush();
s.connection().commit();
s.close();
s = openSession();
master1 = (Master) s.load(Master.class, m1id);
Iterator iter = master1.getIncoming().iterator();
int i=0;
while ( iter.hasNext() ) {
Master m = (Master) iter.next();
assertTrue( "outgoing", m.getOutgoing().size()==1 );
assertTrue( "outgoing", m.getOutgoing().contains(master1) );
s.delete(m);
i++;
}
assertTrue( "incoming-outgoing", i==2 );
s.delete(master1);
s.flush();
s.connection().commit();
s.close();
}
public void testSaveDelete() throws Exception {
Session s = openSession();
Foo f = new Foo();
s.save(f);
s.flush();
s.connection().commit();
s.close();
s = openSession();
s.delete( s.load( Foo.class, f.getKey() ) );
s.flush();
s.connection().commit();
s.close();
}
public void testNewFlushing() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
Baz baz = new Baz();
baz.setDefaults();
s.save(baz);
s.flush();
baz.getStringArray()[0] = "a new value";
Iterator iter = s.iterate("from Baz baz");//no flush
assertTrue( iter.next()==baz );
iter = s.iterate("select elements(baz.stringArray) from Baz baz");
boolean found = false;
while ( iter.hasNext() ) {
if ( iter.next().equals("a new value") ) found = true;
}
assertTrue(found);
baz.setStringArray(null);
s.iterate("from Baz baz"); //no flush
iter = s.iterate("select elements(baz.stringArray) from Baz baz");
assertTrue( !iter.hasNext() );
baz.getStringList().add("1E1");
iter = s.iterate("from Foo foo");//no flush
assertTrue( !iter.hasNext() );
iter = s.iterate("select elements(baz.stringList) from Baz baz");
found = false;
while ( iter.hasNext() ) {
if ( iter.next().equals("1E1") ) found = true;
}
assertTrue(found);
baz.getStringList().remove("1E1");
iter = s.iterate("select elements(baz.stringArray) from Baz baz"); //no flush
iter = s.iterate("select elements(baz.stringList) from Baz baz");
found = false;
while ( iter.hasNext() ) {
if ( iter.next().equals("1E1") ) found = true;
}
assertTrue(!found);
List newList = new ArrayList();
newList.add("value");
baz.setStringList(newList);
iter = s.iterate("from Foo foo");//no flush
baz.setStringList(null);
iter = s.iterate("select elements(baz.stringList) from Baz baz");
assertTrue( !iter.hasNext() );
baz.setStringList(newList);
iter = s.iterate("from Foo foo");//no flush
iter = s.iterate("select elements(baz.stringList) from Baz baz");
assertTrue( iter.hasNext() );
s.delete(baz);
txn.commit();
s.close();
}
public void testFindBySQLAssociatedObjects() throws HibernateException, SQLException {
Session s = openSession();
s.delete("from Assignable");
s.delete("from Category");
Category c = new Category();
c.setName("NAME");
Assignable assn = new Assignable();
assn.setId("i.d.");
List l = new ArrayList();
l.add(c);
assn.setCategories(l);
c.setAssignable(assn);
s.save(assn);
s.flush();
s.connection().commit();
s.close();
s = openSession();
List list = s.createSQLQuery("select {category.*} from category {category}", "category", Category.class).list();
list.get(0);
s.connection().commit();
s.close();
if ( getDialect() instanceof MySQLDialect ) return;
if ( getDialect() instanceof OracleDialect ) return; // todo : this fails on Oracle8 also
s = openSession();
Query query = s.getNamedQuery("namedsql");
assertNotNull(query);
list = query.list();
assertNotNull(list);
Object[] values = (Object[]) list.get(0);
assertNotNull(values[0]);
assertNotNull(values[1]);
assertTrue("wrong type: " + values[0].getClass(), values[0] instanceof Category);
assertTrue("wrong type: " + values[1].getClass(), values[1] instanceof Assignable);
s.connection().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();
}
public void testComponents() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
Foo foo = new Foo();
foo.setComponent( new FooComponent("foo", 69, null, new FooComponent("bar", 96, null, null) ) );
s.save(foo);
foo.getComponent().setName("IFA");
txn.commit();
s.close();
foo.setComponent(null);
s = openSession();
txn = s.beginTransaction();
s.load( foo, foo.getKey() );
assertTrue(
"save components",
foo.getComponent().getName().equals("IFA") &&
foo.getComponent().getSubcomponent().getName().equals("bar")
);
assertTrue( "cascade save via component", foo.getComponent().getGlarch()!=null);
foo.getComponent().getSubcomponent().setName("baz");
txn.commit();
s.close();
foo.setComponent(null);
s = openSession();
txn = s.beginTransaction();
s.load( foo, foo.getKey() );
assertTrue(
"update components",
foo.getComponent().getName().equals("IFA") &&
foo.getComponent().getSubcomponent().getName().equals("baz")
);
s.delete(foo);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
foo = new Foo();
s.save(foo);
foo.setCustom( new String[] { "one", "two" } );
assertTrue( s.find("from Foo foo where foo.custom.s1 = 'one'").get(0)==foo );
s.delete(foo);
txn.commit();
s.close();
}
public void testInsertWithGeneratedVersionAndId() {
// Make sure the env supports bulk inserts with generated ids...
EntityPersister persister = sfi().getEntityPersister( IntegerVersioned.class.getName() );
IdentifierGenerator generator = persister.getIdentifierGenerator();
if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) {
return;
}
Session s = openSession();
Transaction t = s.beginTransaction();
IntegerVersioned entity = new IntegerVersioned( "int-vers" );
s.save( entity );
s.createQuery( "select id, name, version from IntegerVersioned" ).list();
t.commit();
s.close();
Long initialId = entity.getId();
int initialVersion = entity.getVersion();
s = openSession();
t = s.beginTransaction();
int count = s.createQuery( "insert into IntegerVersioned ( name ) select name from IntegerVersioned" ).executeUpdate();
t.commit();
s.close();
assertEquals( "unexpected insertion count", 1, count );
s = openSession();
t = s.beginTransaction();
IntegerVersioned created = ( IntegerVersioned ) s.createQuery( "from IntegerVersioned where id <> :initialId" )
.setLong( "initialId", initialId.longValue() )
.uniqueResult();
t.commit();
s.close();
assertEquals( "version was not seeded", initialVersion, created.getVersion() );
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete IntegerVersioned" ).executeUpdate();
t.commit();
s.close();
}
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();
}
public void testInsertWithGeneratedTimestampVersion() {
// Make sure the env supports bulk inserts with generated ids...
EntityPersister persister = sfi().getEntityPersister( TimestampVersioned.class.getName() );
IdentifierGenerator generator = persister.getIdentifierGenerator();
if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) {
return;
}
Session s = openSession();
Transaction t = s.beginTransaction();
TimestampVersioned entity = new TimestampVersioned( "int-vers" );
s.save( entity );
s.createQuery( "select id, name, version from TimestampVersioned" ).list();
t.commit();
s.close();
Long initialId = entity.getId();
//Date initialVersion = entity.getVersion();
s = openSession();
t = s.beginTransaction();
int count = s.createQuery( "insert into TimestampVersioned ( name ) select name from TimestampVersioned" ).executeUpdate();
t.commit();
s.close();
assertEquals( "unexpected insertion count", 1, count );
s = openSession();
t = s.beginTransaction();
TimestampVersioned created = ( TimestampVersioned ) s.createQuery( "from TimestampVersioned where id <> :initialId" )
.setLong( "initialId", initialId.longValue() )
.uniqueResult();
t.commit();
s.close();
assertNotNull( created.getVersion() );
//assertEquals( "version was not seeded", initialVersion, created.getVersion() );
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete TimestampVersioned" ).executeUpdate();
t.commit();
s.close();
}
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();
}
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();
}
public void testManyToMany() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Container c = new Container();
c.setManyToMany( new ArrayList() );
c.setBag( new ArrayList() );
Simple s1 = new Simple();
Simple s2 = new Simple();
s1.setCount(123); s2.setCount(654);
Contained c1 = new Contained();
c1.setBag( new ArrayList() );
c1.getBag().add(c);
c.getBag().add(c1);
c.getManyToMany().add(s1);
c.getManyToMany().add(s2);
Serializable cid = s.save(c); //s.save(c1);
s.save(s1, new Long(12) ); s.save(s2, new Long(-1) );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c = (Container) s.load(Container.class, cid);
assertTrue( c.getBag().size()==1 );
assertTrue( c.getManyToMany().size()==2 );
c1 = (Contained) c.getBag().iterator().next();
assertTrue( c.getBag().size()==1 );
c.getBag().remove(c1);
c1.getBag().remove(c);
assertTrue( c.getManyToMany().remove(0)!=null );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
c = (Container) s.load(Container.class, cid);
assertTrue( c.getBag().size()==0 );
assertTrue( c.getManyToMany().size()==1 );
c1 = (Contained) s.load( Contained.class, new Long(c1.getId()) );
assertTrue( c1.getBag().size()==0 );
assertTrue( s.delete("from ContainerX c")==1 );
assertTrue( s.delete("from Contained")==1 );
assertTrue( s.delete("from Simple")==2 );
t.commit();
s.close();
}