我现在对一小段代码感到疯狂。我正在创建一个基于 spring MVC 3.2 的简单休息服务,并使用带有 spring-orm 和 hibernate4.1.9 的 JPA 持久层。
我正面临一个关于未映射类的奇怪错误。这是我的第一个实体(在我建立关系之前完美运行)
用户.java
@Entity
@Table(name="user")
public class User {
//[...]
@OneToMany(mappedBy="owner", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private Set<FileInfo> fileInfos;
//[...]
}
文件信息.java
@Entity
@Table(name="file_info")
public class FileInfo {
//[...]
@ManyToOne
@JoinColumn(name = "owner_id")
private User owner;
//[...]
}
小服务程序配置
<?xml version="1.0" encoding="UTF-8"?>
<!--[...]-->
<context:component-scan base-package="org.mypackage.myapp" />
<!--[...]-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="packagesToScan" value="org.mypackage.myapp.model" />
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--[...]-->
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--[...]-->
错误跟踪
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: org.mypackage.myapp.model.User.fileInfos[org.mypackage.myapp.model.FileInfo]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1057)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1597)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
有谁知道问题出在哪里。我对这项技术没有太多经验,我不确定这篇文章中提供的信息是否足够。
感谢帮助 !
编辑 :
这是我的 hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping package="org.mypackage.myapp.model"/>
<mapping class="org.mypackage.myapp.model.User"/>
<mapping class="org.mypackage.myapp.model.FileInfo"/>
<mapping class="org.mypackage.myapp.model.Group"/>
<mapping class="org.mypackage.myapp.model.FileInfoGroup"/>
</session-factory>
</hibernate-configuration>
奥利314