【问题标题】:Spring + Hibernate = Unknown entitySpring + Hibernate = 未知实体
【发布时间】:2009-11-01 16:17:00
【问题描述】:

我正在尝试使用注释将 Spring 与 Hibernate 结合起来,但出现以下错误:

org.springframework.orm.hibernate3.HibernateSystemException : Unknown entity: entities.Bar; nested exception is org.hibernate.MappingException: Unknown entity: entities.Bar

这是我的设置...

我的实体:

package entities;

@Entity    
public class Bar implements Serializable
{
  ...
}

我的豆子:

package blah;

@Repository
@Service("fooService")
@RemotingDestination(channels = { "my-amf" })
public class Foo
{
  protected HibernateTemplate template;

  @Autowired
  public void setSessionFactory(SessionFactory sessionFactory)
  {
    template = new HibernateTemplate(sessionFactory);
  }

  @RemotingInclude
  public void addBar(String name) throws DataAccessException
  {
    Bar bar = new Bar();
    bar.setName(name);
    template.save(bar);
  }

}

我在 Spring 中启用注释:

<context:annotation-config />
<context:component-scan base-package="blah" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:~/blahdb/blahdb" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>entities.Bar</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

当我通过 BlazeDS 从 Flex 应用程序调用 Foo.addBar 方法时出现错误。

我真的很想避免额外的配置,看来这一切都应该工作。

我正在使用 Spring 3.0.0.RC1、Hibernate Annotations 3.4.0、Tomcat 6.0.20 和 Java 1.6.0_15。

有什么想法吗?谢谢。

【问题讨论】:

  • 看起来像一个映射异常,但您显然已经映射了Bar,我会检查是否有任何拼写错误,然后查看您的休眠 jar 是否兼容(使用兼容性矩阵)并与 Spring 3 兼容。
  • 我在 Hibernate 中成功使用了 Spring 3.0.0.RC1:Core 3.3.2.GA,Annotations 3.4.0.GA。所以他们肯定一起工作。
  • James,如果您想通过电子邮件将您项目的相关部分发送给我,我很乐意看一下并将其与我自己的工作项目进行比较,就像我说的听起来很相似。我的电子邮件地址是谷歌电子邮件服务的 concat('willie', '.', 'wheeler')... :-)

标签: java hibernate spring


【解决方案1】:

尝试使用import @javax.persistence.Entity 而不是org.hibernate.annotations.Entity 作为Entity 注释。

【讨论】:

  • 天哪!你摇滚。就是这样。我觉得很蹩脚没有抓住那个。甚至我什至没有在上面的代码中包含导入,因为我认为这不会是问题。
【解决方案2】:

我也遇到了同样的问题,没有找到任何好的答案

对我有用的是在 persistence.xml 文件中声明我的实体类

(资源中和测试中):

<persistence ...>
    <persistence-unit ...>

        <class>com.company.maenad.core.model.News</class>
        <class>com.company.maenad.core.model.AdExtraInfo</class>

    </persistence-unit>
</persistence>

【讨论】:

    【解决方案3】:

    我认为明确编写实体包是安全的。我得到了这个错误并通过使用 EntityScan 注释来解决。

    @EntityScan(basePackages = "your.entities.pakage")
    

    【讨论】:

      【解决方案4】:

      如果用于配置 sessionFactory 的 annotatedClasses 属性未指向包中的正确实体,也会发生上述异常。

      还建议使用属性 packagesToScan 而不是 annotatedClasses,因为它会扫描整个包中的实体,从而避免显式提及具有完全限定类名的实体。

      <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <property name="packagesToScan" value="com.code.entity"></property>
      <property name="hibernateProperties">
      <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hbm2ddl.auto">update</prop>
          <prop key="hibernate.show_sql">true</prop>
      </props>
      </property>
      

      【讨论】:

        【解决方案5】:

        确保您已将正确的命名空间添加到 Spring 应用上下文 XML:

            <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd>
        

        【讨论】:

          【解决方案6】:

          我唯一能想到的是,您的 annotatedClasses 定义以某种方式缺少相关实体。你能仔细检查你的 annotatedClasses def,包括包名吗?

          我认为这个错误是在启动时出现的吗?您能否在错误消息周围包含更多上下文?例如,通过从我的 annotatedClasses 定义中删除其中一个类,我能够重现与您报告的内容类似的内容:

          2009-11-01 10:05:55.593::WARN:  Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invo
          cation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.springinpractice.ch06.model.Message.forum references an unknown entity: com.springinpractice.ch06.model.
          Forum:
          org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.springinpractice.ch06.model.Message.forum references an unknown entity: com.springinpractice.ch06.model.Forum
                  at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
                  at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
          

          [剪辑]

          编辑:另一个问题/想法。您的运行时类路径上是否有适当的注释 JAR(JPA 的 persistence.jar 或 Hibernate 注释 JAR)?

          另一个编辑:另一个。你运行的是哪个 JVM 版本?

          【讨论】:

          • 感谢威利帮助我解决这个问题。我用更多的版本号更新了这个问题。我的 WEB-INF/lib 中有 Hibernate Annotations JAR。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-01
          • 2011-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多