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.
Try using import @javax.persistence.Entity
and not org.hibernate.annotations.Entity
for your Entity
annotation.
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>
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")
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>
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>
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?
精彩评论