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();
分享到:
相关推荐
Hibernate配置文件中配置各种数据库的driver、URL
hibernate配置,详细的讲了hibernate用的配置文件和方法,可以哦
Hibernate配置文件hibernate.cfg.xml中配置信息详解!
逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件
hibernate配置文件 里面提供了连接数据库 数据库使用的方言 是否打印SQL语句 sql语句的格式 以及对象-关系映射文件的地址等
使用反射机制修改加载到内存的Hibernate配置文件,使用该方法可以将数据库的配置信息提取到别的路径下。
struts2+spring+hibernate 配置文件struts2+spring+hibernate 配置文件
hibernate--5.Hibernate配置文件详解
SpringMVC+Spring+hibernate配置
hibernate 配置 C3P0 连接池
如何使用用myeclipse自动生成hibernate配置文件
springboot2.0多数据源集成hibernate配置hibernateDaoSupport, dao层继承hibernateDaoSupport, 不使用jpa方式。
Hibernate配置文件Hibernate配置文件Hibernate配置文件
介绍了hibernate的两种方法,所用myeclipse版本为8.6,数据库为mysql,按步骤即可完成hibernate配置
hibernate所以配置以及简单的介绍
hibernate的配置介绍,在myeclipse中配置hibernate,hibernate已经在myeclipse中了,创建工程,然后在工程中添加hibernate框架,一些设置
JAVA 使用hibernate配置实例,包括普通java工程版本(不是web工程)和maven配置使用hibernate版本。
Hibernate配置详解,详细描述了Hibernate的配置文件,一对一 一对多,多对多的单双向配置详解。
NULL 博文链接:https://hi-beijing.iteye.com/blog/1174299
帮助大家在配置hibernate方面提供支持