hibernate通过session实现增删改查操作实例解析
今天学习了一下如何通过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实现增删改查操作实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅本站:
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
上一篇: 打造酷炫的AndroidStudio插件
下一篇: java 代理机制的实例详解