Spring + Hibernate =未知实体
我正在尝试使用Annotations将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); }
}
我在Spring中启用注释:
entities.Bar org.hibernate.dialect.H2Dialect create
当我通过BlazeDS从Flex应用程序调用我的Foo.addBar方法时,我收到错误。
我真的想避免额外的配置,似乎这一切都应该工作。
我使用的是Spring 3.0.0.RC1,Hibernate Annotations 3.4.0,Tomcat 6.0.20和Java 1.6.0_15。
有任何想法吗? 谢谢。
尝试使用import @javax.persistence.Entity
而不是org.hibernate.annotations.Entity
作为Entity
注释。
我遇到了同样的问题,没有找到任何好的答案
对我有用的是在persistence.xml文件中声明我的实体类
(资源和测试下):
com.company.maenad.core.model.News com.company.maenad.core.model.AdExtraInfo
如果用于配置sessionFactory的annotatedClasses属性未指向包中的正确实体,则上述exception也会出现。
建议使用属性packagesToScan而不是annotatedClasses,因为它扫描整个包的实体,从而避免明确提及具有完全限定类名的实体。
org.hibernate.dialect.MySQLDialect update true
确保您已在Spring应用程序上下文XML中添加了正确的命名空间:
我唯一能想到的是,你的annotatedClasses定义在某种程度上缺少了有问题的实体。 你能仔细检查你的annotatedClasses def,包括包名吗?
我是否认为这个错误会在启动时出现? 你能在错误信息周围加一些上下文吗? 例如,通过从annotatedClasses定义中删除其中一个类,我能够重现类似于您所报告的内容:
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)
[剪断]
编辑:另一个问题/想法。 在运行时类路径上是否有适当的注释JAR(JPA的persistence.jar或Hibernate注释JAR)?
另一个编辑:还有一个。 您运行的JVM版本是什么?
我认为明确写出entite包是安全的。 我收到此错误并使用EntityScan注释解决。
@EntityScan(basePackages = "your.entities.pakage")