【问题标题】:Hibernate 4.3 loads all hbm files even if they're not declared in persistence unitHibernate 4.3 加载所有 hbm 文件,即使它们没有在持久性单元中声明
【发布时间】:2014-06-23 16:18:34
【问题描述】:

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

<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

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

【问题讨论】:

    标签: java spring hibernate jpa


    【解决方案1】:

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

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

    出于我的目的,我需要持久性单元仅包含在&lt;class&gt; 元素中明确列出的实体。为此,我将属性定义为“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 的文档中没有提到它,但这是我使用的版本,我可以确认该属性有效。

    【讨论】:

      【解决方案2】:

      您也可以不使用文件persistence.xml 来配置bean em。例如:

      <bean id="em" 
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
            p:dataSource-ref="dataSource" 
            p:packagesToScan="···.model">
      
          <property name="jpaVendorAdapter">
              <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" 
                    p:showSql="false" 
                    p:databasePlatform="org.hibernate.dialect.SQLServerDialect" />
          </property>
      
          <property name="jpaProperties">
              <props>
                  <prop key="hibernate.format_sql">true</prop>
              </props>
          </property>
      
      </bean>
      

      【讨论】:

        【解决方案3】:

        exclude-unlisted-classes 不起作用,如果您的类在要扫描的包中,例如你想要注册它,但不想要它的 hbm.xml。

        我修复了使用休眠属性hibernate.archive.autodetection 设置为class,使休眠只扫描类而不是映射文件。

        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
        </properties
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-04
          • 1970-01-01
          • 2012-12-19
          • 2023-03-18
          相关资源
          最近更新 更多