您的位置:首页 > 理论基础 > 计算机网络

虚拟化--网络最佳实践

2015-08-19 17:11 471 查看
Seam自带的seam-gen是一个非常好的命令行工具,它自动创建了程序模板,它创建的程序可以直接用Netbeans和Eclipse打开。
但是它创建的数据层却是EntityManager。对于习惯了Hibernate的开发人员来说,不太习惯。本文介绍如何将Seam工程中的EntityManager改成原生Hibernate。

以seam-gen创建的工程myproject为例:

修改components.xml

<!-- 增加hibernate session -->
<persistence:hibernate-session-factory name="hibernateSessionFactory"/>
<persistence:managed-hibernate-session name="hibernateSession"
auto-create="true"
session-factory-jndi-name="java:/hibernateSessionFactory"/>

<!-- 将原来的entityManager注释掉
<persistence:managed-persistence-context name="entityManager"
auto-create="true"
entity-manager-factory="#{myprojectEntityManagerFactory}"/>

<persistence:entity-manager-factory name="myprojectEntityManagerFactory"
persistence-unit-name="myproject"/>
-->

 

增加hibernate.cfg.xml

该文件保存于resources/META-INF

<?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="java:/hibernateSessionFactory">
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.datasource">java:/myprojectDatasource</property>
<property name="hbm2ddl.auto">none</property>
<property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

<property name="transaction.flush_before_completion">true</property>
<property name="connection.release_mode">after_statement</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<!--
<mapping class="com.dfsoft.pm.model.User"/>
-->
</session-factory>
</hibernate-configuration>

 
注意!由于seam 并不会自动flush,因此需要将 hibernate.transaction.flush_before_completion 设置为true。

将resources/META-INF中的persistens** 及orm.xml删除。


修改build.xml

最后一步修改build.xml, 主要内容包括去除原来的persistence.xml,
orm.xml等,加上hibernate.cfg.xml。搜索persistence.xml,
orm.xml将它们注释掉,然后搜索target name="war" depends="compile", 在下面增加

 

<copy tofile="${war.dir}/WEB-INF/classes/hibernate.cfg.xml"
file="${basedir}/resources/META-INF/hibernate.cfg.xml"
overwrite="true"/>

 

 修改完毕!可以开始在seam组件中注入hibernateSession了:

...
@In
Session hibernateSession;
...

Criteria criteria = hibernateSession.createCriteria(Project.class);
...

 注意,一旦采用原生的hibernate,你必须在hibernate.cfg.xml中增加实体类的mapping

 
我的博客:http://www.ubone.cn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: