获取org.hibernate.InvalidMappingException:当我尝试运行程序时无法读取xml

时间:2015-04-15 10:25:13

标签: java hibernate

我是Hibernate的初学者,无法理解我的程序的哪个部分出错了。任何人都可以告诉可能导致此类错误的原因:

org.hibernate.InvalidMappingException: unable to read xml

ManyToManyDemo.java - 它包含持久化类和用于插入数据的Test类。

import java.util.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.Transaction;

class Student {
    private int sid = 0;
    private String sname = null;
    private Set<Course> courses = null;

    public int getSid() {
        return this.sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    } 
    public String getSname() {
        return this.sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public Set<Course> getCourses() {
        return this.courses;
    }
    public void setcourses(Set<Course> courses) {
        this.courses = courses;
    }

    Student(int sid, String sname) {
        setSid(sid);
        setSname(sname);
    }
}

class Course {
    private int cid = 0;
    private String cname = null;
    private Set<Student> students = null;

    public int getCid() {
        return cid;
    } 
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getCname() {
        return this.cname;
    }
    public void setCname(String cname) {
        this.cname=cname;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    Course(int cid, String cname) {
        setCid(cid);
        setCname(cname);        
    }
}

class Test {
    private SessionFactory factory = null;

    public void initSessionFactory() {
        Configuration config = new Configuration().configure("hibernate.cfg.xml");
        ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        factory = config.buildSessionFactory(registry);
    }

    public void createCourseAndStudents() {
        Session session = factory.getCurrentSession();
        Transaction tx = null;

        Set<Student> studentset = new HashSet<Student>();
        Course course = null;

        Student s1 = new Student(5, "Halle Price");
        Student s2 = new Student(3, "William Wick");
        studentset.add(s1);
        studentset.add(s2);

        course = new Course(24, "Django");
        course.setStudents(studentset);

        tx = session.beginTransaction();
        session.save(course);
        tx.commit();
    } 
}

public class ManyToManyDemo {
    public static void main(String[] args) {
      Test t = new Test();
      t.initSessionFactory();
      t.createCourseAndStudents();
    }
}

course.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Course" table="coursetab">
    <id name = "cid" column = "cid">
        <!--<generator class="native"/>-->
    </id>
    <property name= "cname" column = "cname"/>
  </class>
  <set name="students" inverse="false" lazy="false" table="student_course" cascade="all">
        <key column="cid" not-null="true"/>
        <many-to-many column="sid" class="Student"/>
  </set>
</hibernate-mapping>

student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Student" table="studenttab">
    <id name = "sid" column = "sid">
        <generator class="native"/>
    </id>
    <property name= "sname" column = "sname"/>
  </class>
  <set name="courses" inverse = "true" lazy="false" table="student_course" cascade="all">
        <key column="sid" not-null="true"/>
        <many-to-many column="cid" class="Course"/>
  </set>
</hibernate-mapping>

的hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/demodb</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">dbuser</property>
        <property name="connection.password">dbpassword</property>
        <property name="connection.pool_size">0</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="student.hbm.xml" />
        <mapping resource="course.hbm.xml" />
    </session-factory>
</hibernate-configuration>

链接表

create table student_course (cid int not null, sid int not null, primary key(cid, sid), constraint fk_cid foreign key(cid) references coursetab(cid), constraint fk_sid foreign key(sid) references studenttab(sid));

1 个答案:

答案 0 :(得分:1)

您的XML中似乎有错误。如果您正在使用Eclipse(或其他一些IDE),通常会有一个“验证”选项(在Eclipse中,右键单击文本编辑器并选择“验证”)

当我在你的course.hbm.xml文件中这样做时,它说hibernate-mapping标签的内容是错误的。

看起来你已经在班级之外定义了“集合”了吗?

当我将文件更改为此...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Course" table="coursetab">
    <id name="cid" column="cid">
        <!--<generator class="native"/>-->
    </id>
    <property name="cname" column="cname"/>
  <set name="students" inverse="false" lazy="false" table="student_course" cascade="all">
        <key column="cid" not-null="true"/>
        <many-to-many column="sid" class="Student"/>
  </set>
  </class> <!-- I moved this tag here to be below the set element -->
</hibernate-mapping>

我没有验证错误。