Spring + Hibernate =未知实体

Jam*_*ard 14 java spring hibernate

我正在尝试使用Annotations将Spring与Hibernate结合使用,我收到以下错误:

org.springframework.orm.hibernate3.HibernateSystemException : Unknown entity: entities.Bar; nested exception is org.hibernate.MappingException: Unknown entity: entities.Bar
Run Code Online (Sandbox Code Playgroud)

这是我的设置......

我的实体:

package entities;

@Entity    
public class Bar implements Serializable
{
  ...
}
Run Code Online (Sandbox Code Playgroud)

我的豆子:

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);
  }
Run Code Online (Sandbox Code Playgroud)

}

我在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>
Run Code Online (Sandbox Code Playgroud)

当我通过BlazeDS从Flex应用程序调用我的Foo.addBar方法时,我收到错误.

我真的想避免额外的配置,似乎这一切都应该工作.

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

有任何想法吗?谢谢.

小智 34

尝试使用import @javax.persistence.Entity,而不是org.hibernate.annotations.Entity你的Entity注释.


Gal*_*cha 8

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

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

(资源和测试下):

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

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

    </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)


Erk*_*rol 5

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

@EntityScan(basePackages = "your.entities.pakage")
Run Code Online (Sandbox Code Playgroud)