Reputation: 11337
This page suggests !ENTITY:
If you want to avoid duplication, consider using XML entities (for example, [ ] in the DOCTYPE declaration and %allproperties; in the mapping).
The problem is that I can't find anywhere on the web a complete working example.
What I got so far is:
<!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"> ]
>
.. but what about the rest?
1. How EXACTLY do I define the properties in the allproperties.xml
file?
2. How/Where EXACTLY do I include the %allproperties;
keyword (within my <class>
and <union-class>
)?
Upvotes: 3
Views: 1121
Reputation: 9443
This is a basic XML construct called an entity include. You file named 'allproperties.xml' would contain the property mapping fragments of the entities. For example:
<property name="name".../>
<property name="someOtherProperty".../>
<!-- rest of shared property definitions -->
Then in a mapping file you'd say:
<!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>
I defined the <id/>
mapping in each class here, but you can move that into the include file as well if the information is all the same. Anything that is valid as a child of <class/>
will work in the include file.
JAXP expects SYSTEM references to be relative or absolute file references. So above means that the allproperties.xml file would be resolved relative to system identifier of the include file. Often that might not work out so well. To that end, Hibernate also understands a special type of SYSTEM reference prefixed with classpath://. As you might expect that triggers a classpath resource lookup for the referenced resource.
<!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"> ]>
Now, allproperties.xml will be resolved via a classpath lookup using the com/acme/allproperties.xml resource name.
Upvotes: 3