hibernate4的經典總結
阿新 • • 發佈:2019-01-07
Hibernate4 環境搭建:
匯入必須的Hibernate4 JAR包
${hibernate_home}\hibernate-release-4.0.0.Final\lib\required\*
資料庫驅動包(Oracle)
ojdbc14.jar
快取(可選)
${hibernate_home}\hibernate-release-4.0.0.Final\lib\optional\ehcache\*
${hibernate_home}\hibernate-distribution-3.6.0.Final\lib\required\slf4j-api-1.6.1.jar
C3P0連線池(可選)
${hibernate_home\hibernate-release-4.0.0.Final\lib\optional\c3p0\*
編寫hibernate.cfg.xml
Xml程式碼- <?xml version="1.0" encoding="UTF-8"?>
- <!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">oracle.jdbc.driver.OracleDriver</property>
- <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
-
<
- <property name="hibernate.connection.password">tiger</property>
- <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
- <property name="hibernate.show_sql">true</property>
- <property name="hibernate.format_sql">true</property>
- <property name="hibernate.hbm2ddl.auto">none</property>
- <property name="hibernate.jdbc.fetch_size">100</property>
- <property name="hibernate.jdbc.batch_size">30</property>
- <!-- 配置二級快取 -->
- <property name="hibernate.cache.use_second_level_cache">true</property>
- <property name="hibernate.cache.use_query_cache">true</property>
- <!-- Hibernate4 這裡和Hibernate3不一樣 要特別注意!!!-->
- <property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
- <!-- Hibernate3 -->
- <!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
- <!-- 配置C3P0 -->
- <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
- <property name="hibernate.c3p0.max_size">10</property>
- <property name="hibernate.c3p0.min_size">1</property>
- <property name="hibernate.c3p0.max_statements">3</property>
- <property name="hibernate.c3p0.timeout">30</property>
- <property name="hibernate.c3p0.acquire_increment">1</property>
- <property name="hibernate.c3p0.idle_test_periodt">10</property>
- <mapping resource="org/springfuncs/domain/Customer.hbm.xml" />
- <mapping resource="org/springfuncs/domain/Order.hbm.xml" />
- <mapping resource="org/springfuncs/domain/Emp.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
實體類和對映檔案(沒用註解...)
Java程式碼- package org.springfuncs.domain;
- import java.io.Serializable;
- import java.util.Date;
- /** 僱員 */
- public class Emp implements Serializable {
- private static final long serialVersionUID = 2127136225587213245L;
- private Integer empno; // 僱員編號
- private String ename; // 僱員姓名
- private String job; // 工作
- private Integer mgr; // 上級僱員的編號
- private Date hiredate; // 入職日期
- private Double sal; // 薪水
- private Double comm; // 獎金
- private Integer deptno; // 部門編號
- public Emp() {
- }
- public Emp(Integer empno, String ename, Date hiredate, Double sal) {
- this.empno = empno;
- this.ename = ename;
- this.hiredate = hiredate;
- this.sal = sal;
- }
- // getter and setter
- public String toString() {
- return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job
- + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal
- + ", comm=" + comm + ", deptno=" + deptno + "]";
- }
- }
- <?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="org.springfuncs.domain">
- <class name="Emp" table="EMP">
- <cache usage="read-write" /> <!-- 快取 -->
- <id name="empno" column="EMPNO" length="4" type="integer">
- <generator class="assigned" />
- </id>
- <property name="ename" column="ENAME" length="10" type="string" />
- <property name="job" column="JOB" length="9" type="string" />
- <property name="mgr" column="MGR" length="4" type="integer" />
- <property name="hiredate" column="HIREDATE" type="date" />
- <property name="sal" column="SAL" type="double" />
- <property name="comm" column="COMM" type="double" />
- <property name="deptno" column="DEPTNO" length="2" type="integer" />
- </class>
- </hibernate-mapping>
- package org.springfuncs.domain;
- import java.io.Serializable;
- import java.util.HashSet;
- import java.util.Set;
- /** 客戶 */
- public class Customer implements Serializable {
- private static final long serialVersionUID = -6514738924858633212L;
- private Integer id; //id
- private String name; //姓名
- private Set<Order> orders = new HashSet<Order>(); //其下訂單
- public Customer() {
- }
- // getter and setter
- @Override
- public String toString() {
- return "Customer [id=" + id + ", name=" + name + "]";
- }
- }