EntityManager的注入

第一步:服务器数据源发布

第二步:持久化单元配置

persistence.xml 放置到jar包的META-INF(强烈注意千万不要用记事本写persistence.xml 就算另存为UTF-8的格式,记事本会默认在前面加一个你看不见的BOM头,而发布时JBOSS解析XML是不认识它的。所以正确的做法是在eclipse中创建persistence.xml,然后再黏贴过去文本 就不会出问题了,我在这个地方栽过

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

<persistence 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_1_0.xsd" version="1.0">

<persistence-unit name="mysql_pu" transaction-type="JTA">

<jta-data-source>java:MySqlDS</jta-data-source>

<properties>

<property name="hibernate.hbm2ddl.auto" value="update" />

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

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

</properties>

</persistence-unit>

</persistence>

----------------------------------------------------------------------------------------------

ANT (其实eclipse就可以 帮我们打包工程了,所以我一般不用ANT,eclipse自己会用它自己的ANT)

build.xml

<?xml version="1.0"?>

<project name="EJB_JTA" default="ejbjar" basedir=".">

<property environment="env" />

<property name="src.dir" value="${basedir}/src" />

<property name="jboss.home" value="${env.JBOSS_HOME}" />

<property name="jboss.server.config" value="default" />

<property name="build.dir" value="${basedir}/build" />

<!-- Build classpath -->

<path id="build.classpath">

<fileset dir="${jboss.home}/client">

<include name="*.jar" />

</fileset>

<pathelement location="${build.dir}" />

</path>

<target name="undeploy" description="卸载EJB">

<delete file="${jboss.home}/server/${jboss.server.config}/deploy/${ant.project.name}.jar"/>

</target>

<target name="prepare" depends="undeploy">

<delete dir="${build.dir}"/>

<mkdir dir="${build.dir}" />

</target>

<target name="compile" depends="prepare" description="编绎">

<javac srcdir="${src.dir}" destdir="${build.dir}">

<classpath refid="build.classpath" />

</javac>

<!-- -->

<copy file="${src.dir}/META-INF/persistence.xml" todir="${build.dir}/META-INF"/>

</target>

<target name="ejbjar" depends="compile" description="创建EJB发布包">

<jar jarfile="${basedir}/${ant.project.name}.jar" basedir="${build.dir}">

<fileset dir="build">

<include name="*"/>

</fileset>

</jar>

</target>

<target name="deploy" depends="ejbjar" description="发布EJB">

<copy file="${basedir}/${ant.project.name}.jar" todir="${jboss.home}/server/${jboss.server.config}/deploy" />

</target>

</project>

第三步:注入EntityManager (不是线程安全的)

@PersistenceContext(unitName="my_u") //推荐的方式,EJB容器负责EntityManager 的生命周期

protected EntityManager entityManager;

-----------------------------------------------------------------------------------------------------------------------------------------

@PersistenceUnit(unitName="my_u")

protected EntityManagerFactory entityManagerFactory;

@PostConstruct

EntityManager entityManager=entityManagerFactory .createEntityManager();

@PreDestroy

entityManager.close();

你可能感兴趣的:(eclipse,xml,Hibernate,ant,jboss)