gpt4 book ai didi

java - Hibernate 4.3 加载所有 hbm 文件,即使它们没有在持久性单元中声明

转载 作者:行者123 更新时间:2023-11-30 07:07:58 25 4
gpt4 key购买 nike

我正在尝试使用以下配置为带有 JPA 和 Spring 4 的 hibernate 4.3 配置测试:

<bean id="em" class="LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceXmlLocation"
value="classpath:integrations/hibernate4/jpa/persistence.xml" />
</bean>

现在持久化配置如下所示:

<persistence-unit name="test" >
<class>hibernate4.jpa.JpaEntity</class>
<class>hibernate4.jpa.JpaHiLoEntity</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

如果我使用 hibernate 4.2.4 运行上面的一切运行正常,但是使用 4.3 上面的设置加载所有 hbm.xml 资源文件,其中一些引用类路径中不存在的类,导致 ClassNotFoundException

有谁知道改变行为的原因是什么以及如何阻止 hibernate 扫描 hbm.xml 文件(packagesToScan 属性也不起作用)?

最佳答案

我也遇到了这个问题。 hibernate.archive.autodetection 属性控制 Hibernate 将自动扫描的内容:

  1. 如果该属性未定义,将包含 hbm 文件,并且将包含带注释的类,除非 <exclude-unlisted-classes>设置为真。
  2. 如果属性值包含“hbm”,则将包含 .hbm.xml 文件。否则他们不会。
  3. 如果该值包含“class”,则将包括类路径中的任何带注释的类。否则他们不会。
  4. 如果属性被定义,无论其值如何,它都会影响 Hibernate 的行为。请参阅 org.hibernate.jpa.boot.scan.internal.StandardScanOptions 构造函数。

出于我的目的,我需要持久性单元仅包含在 <class> 中明确列出的实体元素。为此,我定义了值为“none”的属性:

<persistence-unit name="num2" transaction-type="RESOURCE_LOCAL">
...
<properties>
...
<property name="hibernate.archive.autodetection" value="none" />
</properties>
</persistence-unit>

不幸的是,我能找到的关于此属性的唯一文档是 stable release ,不管那是什么。 Hibernate 4.3 的文档中没有提及它,但这是我使用的版本,我可以确认该属性有效。

关于java - Hibernate 4.3 加载所有 hbm 文件,即使它们没有在持久性单元中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24370667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com