我現在正爲一小段代碼瘋狂起來。 我創建了一個基於spring MVC 3.2的simpel rest服務,並使用了spring-orm和hibernate4.1.9的JPA持久層。Spring 3.2/Hibernate4.1.9:使用@OneToMany或@ManyToMany以未映射的類錯誤爲目標
我正面臨一個關於未映射類的好奇錯誤。這是我的第一個實體
User.java
@Entity
@Table(name="user")
public class User {
//[...]
@OneToMany(mappedBy="owner", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private Set<FileInfo> fileInfos;
//[...]
}
FileInfo.java
@Entity
@Table(name="file_info")
public class FileInfo {
//[...]
@ManyToOne
@JoinColumn(name = "owner_id")
private User owner;
//[...]
}
servlet配置
<?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>
<!--[...]-->
(之前我建立關係完美的作品)
的Bug跟蹤
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>
ollie314
什麼是在FileInfo中使用的實體註釋的完全限定的名稱? –