`
ruby_java
  • 浏览: 23545 次
  • 性别: Icon_minigender_1
  • 来自: ZZ
社区版块
存档分类
最新评论

hibernate的配置

    博客分类:
  • Java
阅读更多

hibernate目前已经支持JPA,因此可以使用原来hibernate方式配置,也可以使用JPA的方式配置。

一、使用hibernate方式。

    1、 src 目录下建立 hibernate.cfg.xml,简略配置:

 

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


<hibernate-configuration>


<session-factory>


	<!-- Database connection settings -->


	<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>


	<property name="connection.url">jdbc:oracle:thin:@<IP>:1521:<SID></property>


	<property name="connection.username">ibas</property>


	<property name="connection.password">ibas</property>


	<!--   JDBC connection pool c3p0 settings -->	


	<property name="hibernate.c3p0.min_size">5</property>


    <property name="hibernate.c3p0.max_size">20</property>


    <property name="hibernate.c3p0.timeout">1800</property>


    <property name="hibernate.c3p0.max_statements">50</property>


	<!-- JDBC connection pool (use the built-in) 


	<property name="connection.pool_size">1</property>	


	-->


	<!-- SQL dialect -->


 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>


	<!-- Enable Hibernate's automatic session context management -->


	<property name="current_session_context_class">thread</property>


	<!-- Disable the second-level cache -->


	<property name="cache.provider_class">


		org.hibernate.cache.NoCacheProvider


	</property>


	<!-- Echo all executed SQL to stdout -->


	<property name="show_sql">true</property>


	<property name="hibernate.format_sql">true</property>


	<property name="hibernate.use_sql_comments">true</property>


	<!-- Drop and re-create the database schema on startup -->


	<!--  <property name="hbm2ddl.auto">update</property>-->


	<property name="myeclipse.connection.profile">Oracle10g</property>


	<mapping class="com.boc.ibas.model.Translog"/>


     </session-factory>


</hibernate-configuration>

   2、使用 Hibernate 标准方法生成 session 操作,代码:

 

Session session=HiberUtil.getSessionFactory().openSession();


session.beginTransaction();


//TO-DO


session.getTransaction().commit();


session.close();


 二、使用JPA方式。

   1、在src目录下建立persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>


<persistence version="2.0"


	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">


	<persistence-unit name="TestSSH2PU" transaction-type="RESOURCE_LOCAL">


























		<provider>oracle.toplink.essentials.PersistenceProvider</provider>


























		<class>com.hadeslee.jpaentity.Department</class>


























		<class>com.hadeslee.jpaentity.Person</class>


























		<properties>


























			<property name="toplink.jdbc.user" value="sa" />


























			<property name="toplink.jdbc.password" value="hadeslee" />


























			<property name="toplink.jdbc.url"


























				value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH" />


























			<property name="toplink.jdbc.driver"


























				value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />


























			<property name="toplink.ddl-generation" value="create-tables" />


























		</properties>


























	</persistence-unit>


























	<persistence-unit name="TestSSH1PU2" transaction-type="RESOURCE_LOCAL">


























		<provider>org.hibernate.ejb.HibernatePersistence</provider>


























		<class>com.hadeslee.jpaentity.Department</class>


























		<class>com.hadeslee.jpaentity.Person</class>


























		<properties>


























			<property name="hibernate.connection.driver_class"


























				value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />


























			<property name="hibernate.connection.url"


























				value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"></property>


























			<property name="hibernate.connection.username" value="sa"></property>


























			<property name="hibernate.connection.password" value="hadeslee"></property>


























			<property name="hibernate.show_sql" value="true"></property>


























			<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"></property>


























			<property name="hibernate.current_session_context_class"


























				value="thread"></property>


























		</properties>


























	</persistence-unit>


























	<persistence-unit name="TestSSH1ORACLE"


























		transaction-type="RESOURCE_LOCAL">


























		<provider>org.hibernate.ejb.HibernatePersistence</provider>


























		<class>com.boc.ibas.model.Translog</class>


























		<properties>


























			<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />


























			<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@<IP>:1521:<SID>"></property>


























			<property name="javax.persistence.jdbc.user" value="username"></property>


























			<property name="javax.persistence.jdbc.password" value="password"></property>


























			<property name="hibernate.show_sql" value="true"></property>


























			<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"></property>


























			<property name="hibernate.current_session_context_class"


























				value="thread"></property>


























		</properties>


























	</persistence-unit>


























</persistence>


























  2、使用EntityManager操作:

		EntityManagerFactory factory=Persistence.createEntityManagerFactory("TestSSH1ORACLE");








		EntityManager manager=factory.createEntityManager();








		//to-do








		manager.close();








		factory.close();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics