Configuring orm.xml 配置JPA的orm.xml文件

最近折腾EJB3,在使用JPA时候,toplink总是提示@Entity注解被忽略,搞的项目启动不来。仔细研究了一下配置文件,才发现我的orm.xml里竟然写了忽略注解的语句,冤孽啊。

一下内容转自http://hi.baidu.com/wzbg/blog/item/179dc5116fa0f7c2a7ef3ff1.html,详细介绍了orm.xml文件中各个部分的含义及配置方法。

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

<entity-mappings xmlns=”http://java.sun.com/xml/ns/persistence/orm

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance

xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd”

version=”1.0”>

<persistence-unit-metadata>

<xml-mapping-metadata-complete/> <!– 为持久化单元禁用注解数据 –>

<persistence-unit-defaults> <!– 持久化单元默认值 –>

<schema>HR</schema> <!– 设置默认持久化单元模式 –>

<catalog>HR</catalog> <!– 与schema元素极其相似,除了它是提供给支持目录的数据库的 –>

<access>FIELD/PROPERTY</access> <!– 为持久化单元设置默认访问模式 –>

<cascade-persist/> <!– 配置persistence-by-reachability语义 –>

<entity-listeners/> <!– 为持久化单元设置默认实体监听器 –>

</persistence-unit-defaults>

</persistence-unit-metadata>

<package>examples.model</package> <!– 设置默认映射文件包名 –>

<schema>HR</schema> <!– 设置默认持久化单元模式 –>

<catalog>HR</catalog> <!– 与schema元素极其相似,除了它是提供给支持目录的数据库的 –>

<access>FIELD/PROPERTY</access> <!– 设置一个特殊的访问模式作为映射文件默认值,只影响映射文件中定义的托管类 –>

<table-generator name=”empGen” table=”ID_GEN” pk-column-value=”EmpId”/> <!– 用一个表生成器覆盖一个序列生成器 –>

<!– 映射文件中的命名查询 –>

<named-query name=”Employee.findAll”>

<query>SELECT e FROM Employee e</query>

<hint name=”cacheUsage” value=”DoNotUseCache”/>

</named-query>

<named-query name=”findEmpsWithName”>

<query>SELECT e FROM Employee e WHERE e.name LIKE :empName</query>

<hint name=”toplink.cache-usage” value=”DoNotCheckCache”/>

</named-query>

<named-query name=”findEmpsWithHigherSalary”>

<query><![CDATA[SELECT e FROM Employee e WHERE e.salary > :salary]]></query>

</named-query>

<!– 用SQL覆盖一个JPQL查询 –>

<named-native-query name=”findAllManagers” result-class=”Employee”>

<query>

SELECT /+FULL(m)/e.id,e.name,e.salary,e.manager_id,e.dept_id,e.address_id

FROM emp e,(SELECT DISTINCT manager_id AS id FROM emp)m

WHERE e.id = m.id

</query>

</named-native-query>

<!– 指定一个结果集映射 –>

<sql-result-set-mapping name=”DepartmentSummary”>

<entity-result entity-class=”Department”>

<field-result name=”name” column=”DEPT_NAME”/>

</entity-result>

<entity-result entity-class=”Employee”/>

<column-result name=”TOT_EMP”/>

<column-result name=”AVG_SAL”/>

</sql-result-set-mapping>

<entity class=”Employee”>

<!– 覆盖一个表 –>

<table name=”EMP” schema=”HR”/>

<!– 指定从表 –>

<secondary-table name=”EMP_ADDRESS”>

<primary-key-join-column name=”EMP_ID”/>

</secondary-table>

<inheritance strategy=”SINGLE_TABLE/JOINED”/> <!– 覆盖一个继承策略 –>

<discriminator-column name=”TYPE”/> <!– 指定一个鉴别器字段 –>

<!– 指定生命周期回调方法 –>

<pre-persist method-name=”initTransientState”/>

<post-load method-name=”initTransientState”/>

<exclude-default-listeners/> <!– 禁用默认实体监听器 –>

<exclude-superclass-listeners/> <!– 禁用超类实体监听器 –>

<id-class class=”EmployeePK”/> <!– 指定一个标识符类 –>

<attributes>

<id name=”id”>

<!– 覆盖标识符生成器 –>

<generated-value strategy=”TABLE” generator=”EmpGen”/>

<table-generator name=”EmpGen” table=”ID_GEN” pk-column-name=”GEN_NAME”

value-column-name=”GEN_VAL” pk-column-value=”Emp_Gen”/>

<sequence-generator name=”empSeq” sequence-name=”mySeq”/> <!– 定义一个序列生成器 –>

</id>

<embedded-id name=”id”/> <!– 指定一个嵌入式标识符 –>

<!– 覆盖基本映射 –>

<basic name=”name”/>

<basic name=”startDate”>

<temporal>DATE</temporal>

</basic>

<basic name=”comments”>

<column name=”COMM”/>

<lob/>

</basic>

<basic name=”type”>

<column name=”STR_TYPE”/>

<enumerated>ORDINAL/STRING</enumerated>

</basic>

<transient name=”cacheAge”/> <!– 在一个映射文件中设置一个瞬时的数据成员 –>

<version name=”version”/> <!– 指定版本 –>

<!– 覆盖获取方式 –>

<many-to-one name=”address” fetch=”LAZY”/>

<many-to-one name=”manager” fetch=”LAZY”>

<join-column name=”MGR”/>

</many-to-one>

<many-to-one name=”department”/>

<!– 指定一个一对多映射 –>

<one-to-many name=”directs” mapped-by=”manager”>

<order-by/>

</one-to-many>

<!– 一对一主键关联 –>

<one-to-one name=”parkingSpace” mapped-by=”employee”/>

<!– 多对多映射 –>

<many-to-many name=”projects”>

<map-key name=”name”/>

<join-table name=”EMP_PRJ”>

<join-column name=”EMP_ID”/>

<inverse-join-column name=”PROJ_ID”/>

</join-table>

</many-to-many>

<!– 嵌入式映射 –>

<embedded name=”address”>

<!– 继承映射 –>

<attribute-override name=”street”>

<column name=”STREET” table=”EMP_ADDRESS”/>

</attribute-override>

<attribute-override name=”city”>

<column name=”CITY” table=”EMP_ADDRESS”/>

</attribute-override>

<attribute-override name=”state”>

<column name=”PROV” table=”EMP_ADDRESS”/>

</attribute-override>

<attribute-override name=”zip”>

<column name=”PCODE” table=”EMP_ADDRESS”/>

</attribute-override>

</embedded>

</attributes>

<!– 覆盖实体监听器 –>

<entity-listeners>

<entity-listener class=”listeners.EmployeeAuditListener”>

<post-persist method-name=”employeeCreated”/>

<post-update method-name=”employeeUpdated”/>

<post-remove method-name=”employeeRemoved”/>

</entity-listener>

<entity-listener class=”listeners.NameValidator”>

<pre-persist method-name=”validateName”/>

</entity-listener>

<entity-listener class=”listeners.EmployeeExitListener”>

<post-remove method-name=”notifyIT”/>

</entity-listener>

</entity-listeners>

</entity>

<entity class=”FullTimeEmployee”>

<table name=”FT_EMP”/> <!– 连接策略 –>

<discriminator-value>FT</discriminator-value> <!– 指定一个鉴别器值 –>

<attributes>

<basic name=”pension”/>

<basic name=”salary”/>

</attributes>

</entity >

<entity class=”PartTimeEmployee”>

<table name=”PT_EMP”/> <!– 连接策略 –>

<discriminator-value>PT</discriminator-value> <!– 指定一个鉴别器值 –>

<attributes>

<basic name=”hourlyRate”/>

</attributes>

<!– 使用属性和注解覆盖 –>

<attributes-override name=”name”>

<column name=”EMP_NAME”/>

</attributes-override>

<attributes-override name=”salary”>

<column name=”SAL”/>

</attributes-override>

<association-override name=”manager”>

<join-column name=”MGR_NAME” referenced-column-name=”EMP_NAME”/>

</association-override>

</entity >

<!– metadata-complete // 禁用托管类的注解 –>

<entity class=”ParkingSpace” metadata-complete=”true” access=”PROPERTY”>

<attributes>

<id name=”id”/>

<basic name=”lot”/>

<basic name=”location”/>

<!– 一对一主键关联 –>

<one-to-one name=”employee”>

<primary-key-join-column/>

</one-to-one>

</attributes>

</entity>

<entity class=”Department”>

<attributes>

<id name=”id”>

<generated-value strategy=”IDENTITY”/>

</id>

<one-to-many name=”employees” mapped-by=”department”/>

</attributes>

</entity>

<entity class=”Project”>

<attributes>

<id name=”id”>

<generated-value strategy=”IDENTITY”/>

</id>

<basic name=”name”/>

<!– 多对多映射 –>

<many-to-many name=”employees” mapped-by=”projects”/>

</attributes>

</entity>

<embeddable class=”Address”>

<attributes>

<basic name=”street”/>

<basic name=”city”/>

<basic name=”state”/>

<basic name=”zip”/>

</attributes>

</embeddable>

</entity-mappings>


因为我的项目是struts2+spring2.5+JPA,因此采用的是全部annotations驱动,只有数据库配置写了几行xml,所以我的orm.xml就很简洁了:

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

<entity-mappings xmlns=”http://java.sun.com/xml/ns/persistence/orm

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance

xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd

version=”1.0”>

<persistence-unit-metadata>

<!– <xml-mapping-metadata-complete/>–>

<persistence-unit-defaults>

<access>PROPERTY</access>

</persistence-unit-defaults>

</persistence-unit-metadata>

</entity-mappings>

把<xml-mapping-metadata-complete/>这行注释掉就成了。

加载评论框需要翻墙