由于在项目的不同包中存在相同的JavaBean类Student,并且在hibernate.cfg.xml中同时添加了如下映射:
<mapping resource="hibernate/Student.hbm.xml" />
<mapping resource="springHibernate/Student.hbm.xml" />
在运行时抛出如下异常:
Caused by: org.hibernate.DuplicateMappingException: duplicate import: Student refers to both springHibernate.Student and hibernate.Student (try using auto-import="false")
解决方法是在映射文件中添加auto-import="false":
<?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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping auto-import="false"> <class name="springHibernate.Student" table="student" catalog="test"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="200" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" not-null="true" /> </property> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> </class> </hibernate-mapping>