1、Hibernate_0500_CoreAPI
2、Hibernate API 文档需要单独下载
3、Configuration
a)、AnntationConfiguration
b)、进行配置信息的管理
c)、用来产生sessonFactory
d)、可以在configure方法中指定hibernate配置主件
e)、只需关注一个方法即:buildSessionFactory();
5、sessionFactory
a)、用来产生和管理Session
b)、通常情况下每个应用只需要一个SessionFactory
c)、除非要访问多个数据库的情况。
d)、关注两个方法即:openSession、getCurrentSession
1、
对Teacher.java:
package com.bjsxt.hibernate;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
private String yourWifeName;
private Date birthDate;
private boolean good;
private Gender gender;
@Enumerated(EnumType.STRING)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
@Transient
public String getYourWifeName() {
return yourWifeName;
}
public void setYourWifeName(String yourWifeName) {
this.yourWifeName = yourWifeName;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
2、对于HibernateCoreAPITest.java中:
@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
new 一个AnntationConfiguration()调用它的configure()方法,然后会找到hibernate.cfg.xml,找到后会把它的默认信息读出来(数据库连接的信息,和做成的连接池)跟数据库产生连接后再调用buildsessionFactory(),才能够产生sessionFactory().
<!-- Database connection settings -->
<!-- 连接数据库 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- 自带的 连接池,暂不用-->
<property name="connection.pool_size">1</property>
3、对于HibernateCoreAPITest.java中的:
SessionFactory-----用来产生session的工厂,sessionFactory维护的最主要东西为数据库连接池(当它产生session的时候,它会捡一个链接出来交给session)
用getCurrentSession()可以产生一个Session:
Session session = sessionFactory.getCurrentSession();
//也可以用openSession()创建新的session
Session session = sessionFactory.openSession();
【等同于数据库的连接】
用openSession()时,要加session.close();
用getCurrentSession()时,不加session.close();
如:
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
opensession(),永远是打开新的session,需要close();
getCrrentSession是如果当前环境中有的,就会拿环境中有的session.如果没有,建新的:1、用途,界定事务边界。2、事务提交自动close();
4、上下文是在配置文件中指定的:主要有jta和thread
thread主要从数据库来建产它的事务
jta从分步式建产事务,运行时需要application相应的支持(很少用)
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
技巧:
如果hibernate.cfg.xml不叫这个名字(如hibernate.xml),则可以在configure()中指定方向名:
@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure("hibernate.xml").buildSessionFactory();
}
HibernateCoreApITest.java:
package com.bjsxt.hibernate;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class HibernateCoreAPITest {
private static SessionFactory sessionFactory;
@BeforeClass
public static void beforeClass() {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void afterClass() {
sessionFactory.close();
}
@Test
public void testTeacherSave() {
Teacher t = new Teacher();
t.setName("t1");
t.setTitle("middle");
t.setBirthDate(new Date());
// Session session = sessionFactory.openSession();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(t);
Session session2 = sessionFactory.getCurrentSession();
System.out.println(session == session2);
session.getTransaction().commit();
Session session3 = sessionFactory.getCurrentSession();
System.out.println(session == session3);
}
public void testUpdate2() {
Teacher t = new Teacher();
t.setName("zhanglaoshi");
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
session2.update(t);
session2.getTransaction().commit();
}
}
public static void main(String[] args) {
beforeClass();
}
}