标签:
Hibernate的基本数据操作 适用于4.* 。。sessionFactory的创建区别
public class NewsTest { private Session session = null; private SessionFactory sf = null; private Transaction ts = null; @Before public void init() { Configuration config = new Configuration(); config.configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build(); sf = config.buildSessionFactory(sr); session = sf.openSession(); ts = session.beginTransaction(); } @After public void destroy() { ts.commit(); session.close(); sf.close(); } @Test public void testGet() { News news = (News) session.get(News.class, 21); System.out.println(news); } @Test public void testSave(){ News news = new News(); news.setId(10); news.setContent("撒"); news.setTitle("标题"); news.setCreateDate(new Date()); session.save(news); } @Test public void testHQL(){ // String sql = "select new News(n.id, n.title) from News n order by n.id desc"; String sql = "select n.id, n.title from News n order by n.id desc"; Query query = session.createQuery(sql); //query.setString("order", "‘desc‘"); //query.setInteger(0, 43); // query.setParameter(1, 42);//org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 2 // Criteria criteria = session.createCriteria(News.class); // criteria.add(Restrictions.eq("id", 1)); // SQLQuery sqlQuery = session.createSQLQuery(queryString); // sqlQuery.addEntity(News.class); List<News> list = query.list(); System.out.println(list); // List<Object[]> list = query.list(); // for(Object[] news:list){ // for(Object o : news){ // System.out.println(o); // } // } } @Test public void testQueryPage(){ //分页查询 String sql = "from News "; Query query = session.createQuery(sql); int page = 2; int rows = 2; query.setFirstResult((page-1)*rows);//起始行 query.setMaxResults(rows);//最大行数 List<News> list = query.list(); System.out.println(list); } @Test public void testManyToOne(){ //数据操作 Factory factory1 = new Factory("三星2"); Factory factory2 = new Factory("谷歌"); Product product1 = new Product("note3",1230,factory1); Product product2 = new Product("galaxy",2300,factory1); Product product3 = new Product("glass",1340,factory2); //如果工厂与产品保存顺序相反,则结束之后执行update操作 session.save(factory1); session.save(factory2); session.save(product1); session.save(product2); session.save(product3); } @Test public void testGetManyToOne(){ //多对一的关联操作 Product product = (Product)session.get(Product.class, 4); System.out.println("Product name : "+product.getName()); //懒加载,用到才查询 System.out.println("Factory Name : "+product.getFactory().getName()); } @Test public void testGetOneToMany(){ //一对多的关联操作 Factory factory = (Factory)session.get(Factory.class, 2); System.out.println("Product name : "+factory.getName()); System.out.println("Product count : "+factory.getProducts().size()); } @Test public void testDeleteOneToManyCascade(){ Factory factory = (Factory)session.get(Factory.class, 6); session.delete(factory); } }
标签:
原文地址:http://www.cnblogs.com/whytohow/p/5152997.html