在练习Hibernate多对多出现了问题,但是怎么也发现不了问题的所在,很苦恼!
  |---test(包) 
  | |--Course.java 
  | |--ExportDB.java 
  | |--Student.java 
  | |--Test.java 
  | |--Course.hbm.xml 
  | |--Student.hbm.xml 
  |------hibernate.cfg.xml -------------------------- 
hibernate.cfg.xml文件的配置如下: <hibernate-configuration> 
<session-factory> 
    <property name="hibernate.connection.username">scott </property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracle </property> 
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect </property> 
    <property name="hibernate.myeclipse.connection.profile">oracle </property> 
    <property name="hibernate.connection.password">tiger </property> 
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property> 
    <property name="hibernate.show_sql">true </property> 
    <property name="hibernate.format_sql">true </property> 
    <mapping resource="test/Course.hbm.xml" /> 
    <mapping resource="test/Student.hbm.xml" /> 
</session-factory> 
</hibernate-configuration> ------------------------- 
Student.java package test; 
import java.io.Serializable; 
import java.util.HashSet; public class Student implements Serializable{     private long id;    
    private String name;    
    private HashSet courses = new HashSet();    
    public Student(){}    
    public Student(String name,HashSet courses){ 
        this.name = name; 
        this.courses = courses; 
    } 
    public HashSet getCourses() { 
        return courses; 
    } 
    public void setCourses(HashSet courses) { 
        this.courses = courses; 
    } 
    public long getId() { 
        return id; 
    } 
    public void setId(long id) { 
        this.id = id; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    }    

************************ 
Student.hbm.xml 
<hibernate-mapping> 
    <class name="test.Student" table="d_student"> 
        <id name="id" type="java.lang.Long"> 
            <column name="id"> </column> 
            <generator class="sequence"> 
                <param name="sequence">stu_seq </param> 
            </generator> 
        </id> 
        <property name="name" type="java.lang.String"> </property> 
        <set name="courses" table="d_student_course" cascade="all"> 
            <key column="studentid"> </key> 
            <many-to-many class="test.Course" column="courseid" outer-join="true"> </many-to-many> 
        </set> 
    </class> 
</hibernate-mapping> 
---------------------------------- 
Course.java 
package test; import java.io.Serializable; 
import java.util.HashSet; public class Course implements Serializable{     private long id; 
    private String name; 
    private HashSet students = new HashSet(); 
    public Course(){} 
    public Course(String name,HashSet students){ 
        this.name = name; 
        this.students = students; 
    } 
    public long getId() { 
        return id; 
    } 
    public void setId(long id) { 
        this.id = id; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
    public HashSet getStudents() { 
        return students; 
    } 
    public void setStudents(HashSet students) { 
        this.students = students; 
    } 

************************************ 
Course.hbm.xml <hibernate-mapping> 
    <class name="test.Course" table="d_course"> 
        <id name="id" type="java.lang.Long"> 
            <column name="id"> </column> 
            <generator class="sequence"> 
                <param name="sequence">cou_seq </param> 
            </generator> 
        </id> 
        <property name="name" type="java.lang.String" /> 
        <set name="students" table="d_student_course" cascade="all" inverse="true"> 
            <key column="courseid"> </key> 
            <many-to-many class="test.Student" column="studentid" outer-join="true"> </many-to-many> 
        </set> 
    </class> 
</hibernate-mapping> 
----------------------------------------
ExportDB.java 
package test; import org.hibernate.cfg.Configuration; 
import org.hibernate.tool.hbm2ddl.SchemaExport; public class ExportDB {    
    public static void main(String[] args){ 
        Configuration con = new Configuration().configure(); 
        SchemaExport sche = new SchemaExport(con); 
        sche.create(true,true); 
    } 

******************************************** 
运行ExportDB.java之后,可以看见控制台显示如下: 
    drop table d_course cascade constraints     drop table d_student cascade constraints     drop table d_student_course cascade constraints     drop sequence cou_seq     drop sequence stu_seq     create table d_course ( 
        id number(19,0) not null, 
        name varchar2(255 char), 
        primary key (id) 
    )     create table d_student ( 
        id number(19,0) not null, 
        name varchar2(255 char), 
        primary key (id) 
    )     create table d_student_course ( 
        courseid number(19,0) not null, 
        studentid number(19,0) not null, 
        primary key (studentid, courseid) 
    )     alter table d_student_course 
        add constraint FK260A8BDA6972978D 
        foreign key (courseid) 
        references d_course     alter table d_student_course 
        add constraint FK260A8BDA4ECDA955 
        foreign key (studentid) 
        references d_student     create sequence cou_seq     create sequence stu_seq *************************************************** 
Test.java 
package test; import java.util.HashSet; import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; public class Test {     Configuration con; 
    SessionFactory factory; 
    Session session; 
    
    public Test(){ 
        con = new Configuration().configure(); 
        factory = con.buildSessionFactory(); 
    } 
    
    public void insert(){ 
        try{ 
                session = factory.openSession(); 
                Student stu1 = new Student(); 
                Student stu2 = new Student(); 
                HashSet set1 = new HashSet(); 
                HashSet set2 = new HashSet(); 
                Course cou1 = new Course(); 
                cou1.setName("English"); 
                Course cou2 = new Course(); 
                cou2.setName("China"); 
                set1.add(cou1); 
                set1.add(cou2); 
                set2.add(cou1); 
                set2.add(cou2); 
                stu1.setCourses(set1); 
                stu2.setCourses(set2); 
                
                session.save(stu1); 
                session.save(stu2); 
                session.beginTransaction().commit(); 
        }catch(HibernateException hh){ 
                hh.printStackTrace(); 
                session.beginTransaction().rollback(); 
        }finally{ 
                if(session.isOpen()){ 
                    session.close(); 
                }            
        } 
    }    
    public static void main(String[] args){ 
        Test test = new Test(); 
        test.insert(); 
    } 

但是再运行Test.java进行添加数据的时候却出现这个错误,我在网上Google了,但是还是无法查出错误的所在,迫切的希望前辈指点迷津! log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). 
log4j:WARN Please initialize the log4j system properly. 
Hibernate: 
    select 
        stu_seq.nextval 
    from 
        dual 
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of test.Student.setCourses 真诚感谢您的指点~ 

解决方案 »

  1.   

    给代理类设置属性出错,说你的setter方法有问题在test.Student.setCourses 
    Student.java package test; 
    import java.io.Serializable; 
    import java.util.HashSet; public class Student implements Serializable{     private long id;    
        private String name;    
        private HashSet courses = new HashSet(); 
    你的这个set不能用HashSet Hibernate 对Set作了封装,所以你定义最好用set集合,所以你映射可以用set标签,不能用HashSet
      

  2.   

    谢谢您的指教~
    而且,我的insert()方法也没写好~