【问题标题】:Hibernate - spring annotated entities not scanned from within jar休眠 - 未从 jar 中扫描的弹簧注释实体
【发布时间】:2010-10-20 17:20:36
【问题描述】:

我有一个包,其中包含我导入到我的 Web 项目中的带注释的实体类。 当 tomcat 部署项目时,不会扫描 jar 中的实体类以查找注释。有没有办法告诉spring在jar文件中搜索带注释的类?即:

<context:component-scan base-package="{path to jar or something}"/>

【问题讨论】:

  • 我忘了添加 - 我正在使用 maven 创建 jars

标签: hibernate spring


【解决方案1】:

寻找: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

第二个例子:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="packagesToScan" value="test.package"/>
</bean>

在后面加上两个通配符来扫描所有的子包。 (例如:“test.package.**”)

【讨论】:

  • 对于 Hibernate 4:org.springframework.orm.hibernate4.LocalSessionFactoryBean 完成这项工作:)
【解决方案2】:

如果您的意思是 @Entity 带注释的类,&lt;context:component-scan&gt; 与它们无关。 @Entity类是Hibernate发现的,所以需要配置Hibernate,而不是Spring。

如果您通过 JPA 使用 Hibernate(即您有 persistence.xml),则需要将以下行添加到 persistence.xml 以便扫描 /WEB-INF/lib/yourFileWithEntities.jar 中的实体类:

<jar-file>lib/yourFileWithEntities.jar</jar-file>

【讨论】:

  • 我必须添加 WEB-INF/lib/my-package-0.0.1-SNAPSHOT.jar 才能让它工作..跨度>
【解决方案3】:

使用spring3.1,或者使用&lt;class&gt;com.***.***&lt;class&gt;tag 在你的persistence.xml中添加实体类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2016-11-04
    • 2015-12-15
    • 1970-01-01
    • 2021-02-22
    • 2020-03-13
    相关资源
    最近更新 更多