Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Hibernate = Unknown entity

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

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:

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

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.

Any ideas? Thanks.

like image 761
James Ward Avatar asked Nov 01 '09 16:11

James Ward


4 Answers

Try using import @javax.persistence.Entity and not org.hibernate.annotations.Entity for your Entity annotation.

like image 126
Andrew Rubalcaba Avatar answered Nov 11 '22 07:11

Andrew Rubalcaba


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

(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>
like image 25
Gal Bracha Avatar answered Nov 11 '22 09:11

Gal Bracha


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

@EntityScan(basePackages = "your.entities.pakage")
like image 6
Erkan Erol Avatar answered Nov 11 '22 09:11

Erkan Erol


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.

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.

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

like image 3
Praveen Kumar Avatar answered Nov 11 '22 07:11

Praveen Kumar