日期:2014-05-19 浏览次数:20820 次
public List<Test> getAll() { String sql="select CName,DName from Test1 test1,Test2 test2 where test1.Cid=test2.Did"; return (List<Test>)super.find(sql); }
import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Vector; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import y.model.Test; import y.model.Test1; import y.model.Test2; import com.targ.track.financeManager.buildProjectAccount.domain.BuildProjectAccount; import com.targ.track.financeManager.buildProjectAccount.domain.BuildProjectAccountDetail; import com.targ.track.startAppManager.domain.StartApplication; public class MyJunitTest { @org.junit.Test public void testDDL(){ //插入数据 HibernateTest.doInHibernateSession(new InHibernateSession(){ @Override public Object doInHibernateSession(Session session) { Test1 test1 = new Test1(); test1.setCAgo("CAgo"); test1.setCName("CName"); test1.setCPwd("CPwd"); test1.setCSex("CSex"); Test2 test2 = new Test2(); test2.setDAddr("DAddr"); test2.setDAge(123); test2.setDName("DName"); test2.setDPwd("DPwd"); test2.setDSex("DSex"); session.save(test1); session.save(test2); return null; } }); //查询 List<Test> all = (List<Test>) HibernateTest.doInHibernateSession(new InHibernateSession(){ public Object doInHibernateSession(Session session) { String sql="select new y.model.Test(c.CId,c.CName,d.DId,d.DName) from Test1 c,Test2 d where c.CId=d.DId"; Query query = session.createQuery(sql); return query.list(); } }); for(Test test : all){ System.out.println(test); } } public static class HibernateTest { public static Object doInHibernateSession(InHibernateSession doInHibernateSession){ Session session = HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); try{ Object object = doInHibernateSession.doInHibernateSession(session); transaction.commit(); return object; }catch(RuntimeException e){ try{ System.err.println("rollback"); transaction.rollback(); }catch(RuntimeException ex){ System.err.println("回滚失败!&q