我遇到一对多映射问题。如果我不在.xml文件中添加一对多映射,一切正常。但是,当我添加一对多时,我会接受此异常。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring/database/Hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.mkyong.common.App.main(App.java:14)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:610)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:631)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 15 more
Caused by: org.dom4j.DocumentException: Error on line 11 of document : Open quote is expected for attribute "{1}" associated with an element type "class". Nested exception: Open quote is expected for attribute "{1}" associated with an element type "class".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:601)
... 19 more
我的Hibernate.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>/hibernate/User.hbm.xml</value>
<value>/hibernate/Word.hbm.xml</value>
<value>/hibernate/UserWord.hbm.xml</value>
</list>
</property>
</bean>
</beans>
我的User.hbm.xml
<hibernate-mapping>
<class name="com.mkyong.stock.model.User" table="user" catalog="test">
<id name="userId" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<set name="userWords" table="userword" fetch="select">
<key>
<column name="USER_ID" not-null="true"></column>
</key>
<one-to-many class="com.mkyong.stock.model.UserWord"/>
</set>
</class>
</hibernate-mapping>
我的Word.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mkyong.stock.model.Word" table="word" catalog="test">
<id name="wordId" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
</class>
</hibernate-mapping>
我的UserWord.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mkyong.stock.model.UserWord" table="userword" catalog="test">
<id name="userWordId" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="user" class=com.mkyong.stock.model.User fetch="select">
<column name="USER_ID" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>
我的用户bean:
package com.mkyong.stock.model;
import java.io.Serializable;
import java.util.Set;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int userId;
private Set<UserWord> userWords;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public Set<UserWord> getUserWord() {
return userWords;
}
public void setUserWord(Set<UserWord> userWord) {
this.userWords = userWord;
}
}
我的UserWrod bean: 包com.mkyong.stock.model;
public class UserWord implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private int userWordId;
private User user;
private Word word;
public int getUserWordId() {
return userWordId;
}
public void setUserWordId(int userWordId) {
this.userWordId = userWordId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Word getWord() {
return word;
}
public void setWord(Word word) {
this.word = word;
}
}
我的Word Bean:
package com.mkyong.stock.model;
import java.io.Serializable;
import java.util.Set;
public class Word implements Serializable {
private static final long serialVersionUID = 1L;
private int wordId;
private Set<UserWord> userWord;
public int getWordId() {
return wordId;
}
public void setWordId(int wordId) {
this.wordId = wordId;
}
public Set<UserWord> getUserWord() {
return userWord;
}
public void setUserWord(Set<UserWord> userWord) {
this.userWord = userWord;
}
}
我的数据库结构
创建表用户( ID int auto_increment, 主键(ID) );
create table word (
ID int auto_increment,
primary key(ID)
);
create table userword (
ID int auto_increment,
USER_ID int,
WORD_ID int,
primary key(ID)
);
alter table userword
add foreign key (USER_ID)
REFERENCES user(ID);
alter table userword
add foreign key (WORD_ID)
REFERENCES word(ID);
感谢您的帮助,
答案 0 :(得分:0)
quotes
文件中的类缺少UserWord.hbm.xml
。编辑您的UserWord.hbm.xml
,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mkyong.stock.model.UserWord" table="userword" catalog="test">
<id name="userWordId" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<many-to-one name="user" class="com.mkyong.stock.model.User" fetch="select">
<column name="USER_ID" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>