我不知道您是否使用 Spring,但我使用 Spring 功能通过使用packagesToScan属性和mappingLocations属性来获得此行为,如下所示
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<util:list>
<value>classpath:br/com/ar/model/repository/hql.ar.hbm.xml</value>
<value>classpath:br/com/br/model/repository/hql.br.hbm.xml</value>
<value>classpath:br/com/cr/model/repository/hql.cr.hbm.xml</value>
</util:list>
</property>
<property name="packagesToScan">
<util:list>
<value>br.com.ar.model.domain</value>
<value>br.com.br.model.domain</value>
<value>br.com.cr.model.domain</value>
</util:list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
</props>
</property>
</bean>
我希望它可以有用
更新
根据 JPA 规范
由持久性单元管理的托管持久性类集是通过使用以下一项或多项定义的:
- 一个或多个对象/关系映射 XML 文件
- 将搜索类的一个或多个 jar 文件
- 明确的类列表
- 包含在持久单元根中的带注释的托管持久类(除非指定了 exclude-unlisted-classes 元素)
如下
<persistence>
<persistence-unit name="titan">
<!--Explicity list of classes-->
<class>br.com.ar.model.domain.A</class>
<class>br.com.ar.model.domain.B</class>
<!--Set up any jar file by using jar-file element-->
<!--Its value is a path relative to the JAR file that contains persistence.xml-->
<jar-file>../lib/customer.jar</jar-file>
<!--ORM mapping file-->
<!--It may be present anywhere on the class path-->
<mapping-file>mapping.xml</mapping-file>
<properties>
Properties goes here
</properties>
</persistence-unit>
</persistence>