hibernate通过session实现增删改查操作 hibernate通过session实现增删改查操作实例解析
一勺言 人气:0今天学习了一下如何通过hibernate来实现对数据库的增删改查,下面来简单介绍一下:
首先创建个Student类:
package com.hibernate.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "_Teacher") public class Teacher { private int id; private String name; private int age; private String birthPlace; @Id @GeneratedValue @Column(name = "_id") public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "_name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "_age") public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Column(name = "_birthPlace") public String getBirthPlace() { return birthPlace; } public void setBirthPlace(String birthPlace) { this.birthPlace = birthPlace; } }
然后创建个JUnit Test Case类型的TeacherTest:
先讲讲session.save()方法,也就是增:
package com.hibernate.model; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class TeacherTest { public static SessionFactory sf = null; @BeforeClass public static void beforeClass(){ try{ sf = new AnnotationConfiguration().configure().buildSessionFactory(); //此处最好要获取一下异常,因为annotation有一个bug有时出现有时不出现 } catch(Exception e){ e.printStackTrace(); } finally{ } } @Test public void testSave() { //增 Teacher t = new Teacher(); t.setAge(23); t.setName("moluo"); t.setBirthPlace("huangshan"); Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); } @AfterClass public static void afterClass(){ sf.close(); } }
亮结果:
再讲讲session.delete()方法,也就是删:
@Test public void testDelete() { //删 Teacher t = new Teacher(); t.setId(2); Session session = sf.getCurrentSession(); session.beginTransaction(); session.delete(t); session.getTransaction().commit(); }
亮结果:
接着再来session.get(),也就是查:
@Test public void testGet() {//get Session session = sf.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.get(Teacher.class, 1); System.out.println("姓名为:"+t.getName()); session.getTransaction().commit(); }
亮结果:
Hibernate: select teacher0_._id as column1_1_0_, teacher0_._age as column2_1_0_, teacher0_._birthPlace as column3_1_0_, teacher0_._name as column4_1_0_ from _Teacher teacher0_ where teacher0_._id=? 姓名为:moluo
再来另一种查询方式:session.load():
@Test public void testLoad() {//load Session session = sf.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.load(Teacher.class, 1); System.out.println("姓名为:"+t.getName()); session.getTransaction().commit(); }
亮结果:
Hibernate: select teacher0_._id as column1_1_0_, teacher0_._age as column2_1_0_, teacher0_._birthPlace as column3_1_0_, teacher0_._name as column4_1_0_ from _Teacher teacher0_ where teacher0_._id=? 姓名为:moluo
这里解释一下这俩查询之间的区别:首先当要查询的对象不存在的时候,返回的信息是不同的。get方式会返回:java.lang.NullPointerException
load方式会返回:org.hibernate.ObjectNotFoundException:Norowwiththegivenidentifierexists
其次,load返回的是代理对象,等真正用到的时候才会发出SQL语句;另外get是直接从数据库里加载数据,不存在延迟。
最后再讲讲最常用的更新方式,通过HQL语句来更新:
@Test public void testUpdate() {//更新 Session session = sf.getCurrentSession(); session.beginTransaction(); String url = "update Teacher t set t.birthPlace = 'anhui' where id = 1"; //注意这里的Teacher必须是对象名字,而不是表名,t是对象的别名 Query q = session.createQuery(url); //这里导入的包是:import org.hibernate.Query; q.executeUpdate(); session.getTransaction().commit(); }
亮结果:
通过更新,把摩罗我的籍贯从黄山更新成安徽了....
其实本身有session.update()这个更新方法的,但只是这个更新方法如果你每次只更新一部分列的话,这种更新方式也会把所有列都更新一遍,效率不是太高,所以就不怎么提倡使用,当然也有弥补方式,比如如果使用的是xml的话,可以在配置文件中某个不想被更新的列的property标签里设置update="false";另外也可以在配置文件的class后面设置动态更新:dynamic-update="true";当然如果使用的是Annotation,可以在不想更新的列上设置@column(update="false");其实这些都不灵活,使用HQL才是最好的,所以这里就只贴出HQL更新的代码。
总结
以上就是本文关于hibernate通过session实现增删改查操作实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅本站:
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
加载全部内容