views:

1696

answers:

3

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.

+1  A: 

Make sure that you've added the proper namespaces to your Spring app context 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&gt;
duffymo
I already have those. Thanks though.
James Ward
+1  A: 

Hi James. 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?

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:

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?

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

Willie Wheeler
Thanks Willie for helping me with this. I've updated the question with more version numbers. I have the Hibernate Annotations JARs in my WEB-INF/lib.
James Ward
+3  A: 

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

Andrew Rubalcaba
OMG! You rock. That was totally it. I feel so lame for not catching that. And even lamer that I didn't even include the imports in the code above because I didn't think that would be the problem.
James Ward
Awesome. 300 reputation points!
Andrew Rubalcaba