0

I am a beginner to Hibernate and cannot understand in which part of my program I have made an error. Can anyone please tell what may cause this type of error:

org.hibernate.InvalidMappingException: unable to read xml

ManyToManyDemo.java -- it contains the persistant classes and a Test class for inserting data.

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>

link table

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));
0

1 Answer 1

1

It looks like you have an error in your XML. If you are using Eclipse (or some other IDE), there is usually a "validate" option (in Eclipse, right click on the text editor and choose "Validate")

When I did this on your course.hbm.xml file, it said that the contents of the hibernate-mapping tag were wrong.

It looks like you have defined the "set" outside of the class?

When I changed your file to this...

<?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>

I got no validation errors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.