关于使用Hibernate 的HQL 语句进行增删改查
2013-03-26 21:29
309 查看
因为在hibernate中已经封装好了增删改查,但是要自己实现sql语句的增删改查怎么办呢?HQL是面向对象的结构化查询语句所以在hql1里面的News是你定义的pojo。
最后的system.out.println()是来进行测试的。
//实例化Configuration 加载hibernate.cfg.xml Configuration conf = new Configuration().configure(); //创建SessionFactory SessionFactory sf = conf.buildSessionFactory(); Session sess = sf.openSession(); Transaction tx = sess.beginTransaction(); //下面要实现的是更新操作。 String hql1 = "update News set title='我是老虎' where content=32323"; //hibernate3 没有insert 语句 但是通过这种SQL形式可以实现 String hql2 = "insert into news_table (id,title,content) values('4','sdfs','sadfsdfew')"; //这里是查询语句 String hql3 = "from News"; //这里是删除语句 String hql4 = "delete News where content=32323"; Query q1 = sess.createQuery(hql1); Query q2 = sess.createSQLQuery(hql2); Query q4 = sess.createQuery(hql4); @SuppressWarnings("unchecked") List<News> list1 = (List<News>)sess.createQuery(hql3).list(); //q1.setInteger(0, 32323); q1.executeUpdate(); q2.executeUpdate(); q4.executeUpdate(); System.out.println(list1.size()); System.out.println(list1.get(0).getContent()); tx.commit(); sess.close();
最后的system.out.println()是来进行测试的。
相关文章推荐
- Java Hibernate中使用HQL语句进行数据库查询的要点解析
- hibernate 使用HQL语句进行多表联查
- 关于hibernate HQL不能使用insert语句
- Hibernate(3)-使用HQL语句进行数据查询
- Eclipse使用hibernate进行HQL语句查询时,速度慢的原因之一
- 在hibernate中使用HQL语句和QBC两种方法进行查询
- JPA之使用JPQL语句进行增删改查
- hibernate里使用hql语句处理多表查询
- hibernate使用带有sum()的hql语句出错的问题
- Hibernate之关于HQL语句参数的绑定
- 如何在hibernate+mySQL中使用HQL的cast语句
- hibernate中hql语句的基本使用
- hibernate执行hql语句进行查询时,如果实体类有子类不仅会查询该类而且也会查询子类(子类在hbm中进行了配置)
- hibernate使用hql执行查询操作时,多出update语句
- 使用Query进行HQL语句查询和SQL语句查询
- 使用hibernate的hql语句做查询报错 org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped
- hibernate使用hql和sql查询总记录数语句
- hibernate之大批量和批量操作(使用HQL的大批量语句)
- Hibernate中使用HQL进行查询操作
- Hibernate基础-使用HQL进行查询