Java Spring + Hibernate = 未知实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1657557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:46:06  来源:igfitidea点击:

Spring + Hibernate = Unknown entity

javahibernatespring

提问by James Ward

I'm trying to combine Spring with Hibernate using Annotations and I'm getting the following error:

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

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

Here is my setup...

这是我的设置...

My Entity:

我的实体:

package entities;

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

My Bean:

我的豆:

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);
  }

}

}

I'm enabling annotations in Spring:

我在 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>

I get the error when I call my Foo.addBar method from a Flex application through BlazeDS.

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

I'd really like to avoid additional configuration and it seems this should all work.

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

I'm using Spring 3.0.0.RC1, Hibernate Annotations 3.4.0, Tomcat 6.0.20, and Java 1.6.0_15.

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

Any ideas? Thanks.

有任何想法吗?谢谢。

采纳答案by Andrew Rubalcaba

Try using import @javax.persistence.Entityand not org.hibernate.annotations.Entityfor your Entityannotation.

尝试使用import @javax.persistence.Entityand notorg.hibernate.annotations.Entity用于您的Entity注释。

回答by duffymo

Make sure that you've added the proper namespaces to your Spring app context XML:

确保您已将正确的命名空间添加到 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>

回答by duffymo

The only thing I can think of is that somehow your annotatedClasses definition is missing the entity in question. Can you double-check your annotatedClasses def, including package names?

我唯一能想到的是,不知何故,您的 annotatedClasses 定义缺少相关实体。你能仔细检查你的 annotatedClasses def,包括包名吗?

Am I right in thinking that this error is coming up on startup? Can you include a little more of the context around the error message? For example, I was able to reproduce something similar to what you are reporting by removing one of the classes from my annotatedClasses definition:

我认为这个错误是在启动时出现的吗?您能否在错误消息周围包含更多上下文?例如,通过从我的 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)

[snip]

[剪辑]

EDIT:Another question/idea. Do you have the appropriate annotations JAR (either persistence.jar for JPA or the Hibernate annotations JAR) on your runtime classpath?

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

ANOTHER EDIT:One more. What JVM version are you running?

另一个编辑:还有一个。您正在运行什么 JVM 版本?

回答by Gal Bracha

I've encountered the same problem and didn't find any good answer for this

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

What worked for me was to declare my entity classes in the persistence.xml file

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

(Both under resources and under Test):

(在资源和测试下):

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

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

    </persistence-unit>
</persistence>

回答by Praveen Kumar

The above mentioned Exception also occours if the annotatedClasses property used to configure the sessionFactory are not pointing towards the right Entities in the package.

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

It is also advisable to use the property packagesToScan instead of annotatedClasses as it scans the entire package for Entities thus avoids explicit mention of Entities with fully qualified class names.

还建议使用属性 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>

回答by Erkan Erol

I think writing explicitly the package of entites is safe. I got this error and solve by using EntityScan annotation.

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

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