简体   繁体   English

Spring + Hibernate =未知实体

[英]Spring + Hibernate = Unknown entity

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. 谢谢。

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

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>

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>

I think writing explicitly the package of entites is safe. 我认为明确写出entites包是安全的。 I got this error and solve by using EntityScan annotation. 我收到此错误并使用EntityScan注释解决。

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

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>

The only thing I can think of is that somehow your annotatedClasses definition is missing the entity in question. 我唯一能想到的是,你的annotatedClasses定义在某种程度上缺少了有问题的实体。 Can you double-check your annotatedClasses def, including package names? 你能仔细检查你的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版本是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM