弹簧 + 休眠 = 未知实体

2022-09-03 08:35:19

我正在尝试使用注释将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);
  }

}

我在春季启用注释:

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

有什么想法吗?谢谢。


答案 1

尝试使用 而不是用于注释。import @javax.persistence.Entityorg.hibernate.annotations.EntityEntity


答案 2

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

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

(在资源和测试中):

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

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

    </persistence-unit>
</persistence>

推荐