首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

J2EE之路(5)hibernate基础

2013-12-02 
J2EE之路(五)hibernate基础二:要导入的包(三组)以下是解压hibernate-3.2.5.ga.zip之后的包目录??把hiberna

J2EE之路(五)hibernate基础

二:要导入的包(三组)

以下是解压hibernate-3.2.5.ga.zip之后的包目录


J2EE之路(5)hibernate基础
?
?把hibernate3.jar和lib文件夹下的所有jar包导入到工程内,当然还不要忘了jdbc包

?

User.java

package hibernate.bean;import java.util.Date;public class User {private int id;private String name;private Date birthday;/** * @return the id */public int getId() {return id;}/** * @param id the id to set */public void setId(int id) {this.id = id;}/** * @return the name */public String getName() {return name;}/** * @param name the name to set */public void setName(String name) {this.name = name;}/** * @return the birthday */public Date getBirthday() {return birthday;}/** * @param birthday the birthday to set */public void setBirthday(Date birthday) {this.birthday = birthday;}}

User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="hibernate.bean"><class name="User"><id name="id"><generator name="code"><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///test</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password"></property><property name="hbm2ddl.auto">create</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><mapping resource="hibernate/bean/User.hbm.xml"/></session-factory></hibernate-configuration>

?Base.java

package hibernate;import java.util.Date;import hibernate.bean.User;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.Session;public class Base {public static void main(String[] args) {Configuration cfg = new Configuration();cfg.configure();SessionFactory sf = cfg.buildSessionFactory();Session s = sf.openSession();Transaction t = s.beginTransaction();User user = new User();user.setName("test201");user.setBirthday(new Date());s.save(user);t.commit();s.close();}}

?

java类(实体类) 必须是默认的无参数构造方法

尽量有一个id,去对应数据库的主键,否则hibernate很多功能实现不了

最关键的事映射文件这个文件把对象和数据库映射起来

Session和HttpSession是一点关系都没有的

?

Session接口及get-load-persist方法

Session是hibernate中一个非常核心的借口,完成增删改查等操作

clear是用来清缓存的

close用来关闭连接

delete删除一个对象

save保存一个对象

get从数据库中根据主键拿一条记录

?

static User getUser(int id){Session s = null;try{s = HibernateUtil.getSession();User user = (User)s.get(User.class,id);return user;}finally{if(s!=null)s.close();}}

User user = (User)s.get(User.class,id);?

传给hibernate实体对象,它就知道在数据库中对应哪张表,好厉害。

?load()方法就是所谓软加载,不会立即执行SQL而是等第一次用这个对象的时候才去执行sql

merge()方法就是更新

persist()方法也是保存区别是persist方法没开启事物的话,根本不会插入,而save方法没开启事物的话是先插入再回滚。

update顾名思义是更新

lock查询一条数据,并且把这条数据加锁不允许别人修改

saveOrUpdate()方法:让hibernate自己判断是保存还是更新,看id有没有值。与merge的区别是,saveOrUpdate之后对象会变成持久的,而merge之后对象是托管的。

对象的三种状态:

瞬时(transient):数据库中没有数据与之对应,超过作用域会被垃圾回收器回收,一般是new出来的并且与session没有关联的对象

持久(persistent):数据库中有数据与其相对应,当前与session有关联,并且相关联的session没有关闭,事物没有提交持久事物状态发生改变,在提交事物时会影响到数据库(hibernate能检测到)

脱管(detached):数据库中有数据与之对应,但当前没有session与之关联:托管对象发生改变hibernate不能检测到。


J2EE之路(5)hibernate基础
?

热点排行