5.1.1.1. EntityResolver

As mentioned previously, Hibernate will first attempt to resolve DTDs in its classpath. The manner in which it does this is by registering a custom org.xml.sax.EntityResolver implementation with the SAXReader it uses to read in the xml files. This custom EntityResolver recognizes two different systemId namespaces. 如前所述,Hibernate首先在其classpath中查找DTD。其行为是依靠在系统中注册的org.xml.sax.EntityResolver的一个具体实现,SAXReader依靠它来读取xml文件。这一 EntityResolver 实现能辨认两种不同的 systenId命名空间。

  • 若resolver遇到了一个以http://hibernate.sourceforge.net/为开头的systemId,它会辨认出是hibernate namespace,resolver就试图通过加载Hibernate类的classloader来查找这些实体。

  • 若resolver遇到了一个使用classpath://URL协议的systemId,它会辨认出这是user namespace,resolver试图通过(1)当前线程上下文的classloader和(2)加载Hibernate class的classloader来查找这些实体。

使用user namespace(用户命名空间)的例子:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" [
    <!ENTITY types SYSTEM "classpath://your/domain/types.xml">
]>

<hibernate-mapping package="your.domain">
    <class name="MyEntity">
        <id name="id" type="my-custom-id-type">
            ...
        </id>
    <class>
    &types;
</hibernate-mapping>

types.xmlyour.domain包中的一个资源,它包含了一个定制的第 5.2.3 节 “自定义值类型”