日期:2014-05-19  浏览次数:20625 次

hibernate三表连接,插入数据。求指教
看在我这么晚还在学习的份上。大家给点鼓励吧。。能帮我解决问题是对我最好的鼓励。。感激不尽。。

最近在学hibernate。学到映射关系,想自己综合的练练手。但是插入数据抛了一个异常:
Java code
org.hibernate.MappingException: Unknown entity: java.util.HashSet
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1366)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
    at com.test.bean.TestHibernate.main(TestHibernate.java:79)


先看看pojo类:胜略getter,setter方法了。
主要关系是:课程与学生是多对多的关系。班级与学生是一对多的关系
Java code

        /**
     * 学生类
     */
    private String id;
    private String name;
    private int age;
    private Set<Course> course;
        /**
     * 班级类
     */
    private String id;
    private String name;
    private Set<Student> student;
        /**
     * 课程类
     */
    private String id;
    private String name;
    private Set<Student> student;

再看看映射文件:
student.hbm.xml:
Java code

<class name="com.test.bean.Student" table="student">
        <id name="id" column="id" type="string">
            <generator class="uuid"/>
        </id>
        <property name="name" column="studentName" type="string"/>
        <property name="age" column="Studentage" type="int" />
        <set name="course" table="student_course" cascade="all" >
            <key column="student_id"/>
            <many-to-many class="com.test.bean.Course" column="course_id" />
        </set>
    </class>

course.hbm.xml
Java code
<class name="com.test.bean.Course" table="course">
        <id name="id" column="id" type="string">
            <generator class="uuid"/>
        </id>
        <property name="name" column="courseName" type="string"/>
        <set name="student" table="student_course" cascade="all">
            <key column="student_id"/>
            <many-to-many class="com.test.bean.Student" column="course_id"  />
        </set>
    </class>

还有一个team.hbm.xml