首先把該放的jar檔案都放到CLASSPATH內,稍作修改程式碼,以及Hibernate定義後,
就可以使用Hibernate3+ehcache+c3p0+jtds+MSSQL
Hibernate資料模型物件撰寫範例(只貼上Parent端)
<?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 default-lazy="true" package="com.pcyi.DAO"> <class lazy="true" name="Parent" table="walkone_parent"> <cache usage="read-write" /> <id column="id" name="id" type="integer"> <generator class="native" /> </id> <!-- Parent 物件關聯 Children --> <set cascade="all" inverse="true" name="children" order-by="id DESC" lazy="true"> <key column="ref_parent_id" /> <one-to-many class="Children" /> </set> </class></hibernate-mapping>
package com.pcyi.DAO;import java.io.Serializable;import java.util.Set;/*** Parent 物件,有Children子集,關係one to many** @hibernate.class table="walkone_parent"* @hibernate.cache usage="read-write"*/public class Parent implements Serializable{ private static final long serialVersionUID = 1L; private Integer id; private Set<Children> children; public static long getSerialVersionUID() { return serialVersionUID; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } /** * 回傳子集合 * @hibernate.set cascade="all" inverse="true" * @hibernate.collection-key column="ref_parent_id" * @hibernate.collection-one-to-many class="com.pcyi.DAO.Children" * @hibernate.cache usage="read-write" */ public Set<Children> getChildren() { return this.children; } public void setChildren(Set<Children> children) { this.children = children; }}
Hibernate設定內容
<?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 name="walkOne"> <!-- 使用MSSQL的連接器 --> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- JDBC的Driver --> <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <!-- JDBC連接MSSQL字串 --> <property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/dbname;charset=utf-8</property> <!-- 資料庫帳號 --> <property name="connection.username">id</property> <!--資料庫密碼--> <property name="connection.password">pw</property> <!-- 二級快取設定開始 --> <!-- 使用EhCacheProvider --> <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 快取SQL語法 --> <property name="hibernate.cache.use_query_cache">true</property> <!-- 不在Log內顯示SQL語法 --> <property name="show_sql">false</property> <!-- 不格式化SQL語法 --> <property name="format_sql">false</property> <!-- 二級快取設定結束 --> <!-- 連接池設定開始 --> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="c3p0.acquire_increment">0</property> <!-- 100秒 --> <property name="c3p0.idle_test_period">100</property> <property name="c3p0.max_size">100</property> <property name="c3p0.max_statements">0</property> <property name="c3p0.min_size">10</property> <!-- 300秒 --> <property name="c3p0.timeout">300</property> <!--連接池設定結束--> </session-factory></hibernate-configuration>
ehcache設定內容
<?xml version="1.0" encoding="UTF-8"?><ehcache> <!-- 快取檔案存在java.io.tmpdir之下的ehcache/app1資料夾內,方便不同webapp檔案管理 --> <diskStore path="java.io.tmpdir/ehcache/app1"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="5" eternal="false" timeToLiveSeconds="120" overflowToDisk="true" /> <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="false" overflowToDisk="true" /></ehcache>
全站熱搜
留言列表