This page建议!实体:
If you want to avoid duplication, consider using XML entities (for example, [ ] in the DOCTYPE declaration and %allproperties; in the mapping).
问题是我在网络上找不到完整的工作示例。
到目前为止我得到的是:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
[ <!ENTITY allproperties SYSTEM "allproperties.xml"> ]
>
..但是剩下的呢?
1. 我如何准确定义 allproperties.xml
中的属性文件?
2. 我如何/在哪里确切地包含 %allproperties;
关键字(在我的 <class>
和 <union-class>
内)?
最佳答案
这是一个称为实体包含的基本 XML 构造。名为“allproperties.xml”的文件将包含实体的属性映射片段。例如:
<property name="name".../>
<property name="someOtherProperty".../>
<!-- rest of shared property definitions -->
然后在映射文件中你会说:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" [
<!ENTITY allproperties SYSTEM "allproperties.xml"> ]>
<hibernate-mapping>
<class name="Class1" ...>
<id .../>
&allproperties;
</class>
<class name="Class2" ...>
<id .../>
&allproperties;
</class>
</hibernate-mapping>
我定义了<id/>
此处每个类中的映射,但如果信息全部相同,您也可以将其移至包含文件中。作为 <class/>
的子项有效的任何内容将在包含文件中工作。
JAXP 期望 SYSTEM 引用是相对或绝对文件引用。因此,上面意味着 allproperties.xml 文件将相对于包含文件的系统标识符进行解析。通常情况下,结果可能不太好。为此,Hibernate 还理解一种以 classpath://为前缀的特殊类型的 SYSTEM 引用。正如您可能期望的那样,这会触发对引用资源的类路径资源查找。
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" [
<!ENTITY allproperties SYSTEM "classpath://com/acme/allproperties.xml"> ]>
现在,将使用 com/acme/allproperties.xml 资源名称通过类路径查找来解析 allproperties.xml。
关于java - hibernate !实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997522/