2012-05-29 109 views
2

我是新來的休眠。我有三個表(課程 - 學生課程的學生)。以下是我的代碼,但它沒有工作。我也寫了cfg.xml。我認爲我的問題是我的HBM文件,但我無法解決。多對多在休眠

public class Student { 
private int student_id; 
private String student_name; 
public Student() 
{ 

} 
public Student(int id,String name) 
{ 
this.student_id=id; 
this.student_name=name; 
} 
private Set<Course> courses = new HashSet<Course>(); 
public int getstudentid() 
{ 
    return student_id; 
} 
public void setstudentid(int id) 

{ 
    this.student_id=id; 
} 
public String getstudentname() 
{ 
    return student_name; 
} 
public void setstudentname(String name) 
{ 
    this.student_name=name; 
} 
public Set<Course> getcourse() 
{ 
    return courses; 
} 
public void setcourse(Set<Course> courses) 
{ 
    this.courses=courses; 
} 
} 

public class Course { 
    private int course_id; 
    private String course_name; 
    private Set<Student> students= new HashSet<Student>(); 
public Course() 
{ 

} 
public Course(int id, String name) 
{ 
    this.course_id=id; 
    this.course_name=name; 
} 
public int getcourseid() 
{ 
    return course_id; 
} 
public void setcourseid(int id) 
{ 
    this.course_id=id; 
} 
public String getcoursename() 
{ 
    return course_name; 
} 
public void setcoursename(String name) 
{ 
    this.course_name=name; 
} 
public Set<Student> getstudents() 
{ 
    return students; 
} 
public void setstudents(Set<Student> students) 
{ 
    this.students=students; 
} 
} 

的hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated May 29, 2012 3:19:54 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping package="first"> 
    <class name="Course" table="COURSE"> 
     <id name="course_id" type="int" access="field"> 
      <column name="COURSE_ID" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="course_name" type="java.lang.String" access="field"> 
      <column name="COURSE_NAME" /> 
     </property> 
     <set name="students" table="student_course" 
inverse="false" lazy="true" fetch="join" cascade="all"> 
<key column="student_id" /> 
<many-to-many column="course_id" class="Course" /> 
</set> 
    </class> 
</hibernate-mapping> 

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated May 29, 2012 3:19:54 PM by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping package="first"> 
    <class name="Student" table="STUDENT"> 
     <id name="student_id" type="int" access="field"> 
      <column name="STUDENT_ID" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="student_name" type="java.lang.String" access="field"> 
      <column name="STUDENT_NAME" /> 
     </property> 
     <set name="courses" table="student_course" 
inverse="false" lazy="true" fetch="join" cascade="all"> 
<key column="student_id" /> 
<many-to-many column="course_id" class="Course" /> 
</set> 
    </class> 
</hibernate-mapping> 

我的託管代碼:

public class ManageStudent { 
    private static SessionFactory sf; 
    private static ServiceRegistry serviceRegistry; 

    public static void main(String[] args) { 
    try { 
    Configuration configuration = new Configuration().addResource("first/Student.hbm.xml").addResource("first/Course.hbm.xml"); 
    configuration.configure(); 
    serviceRegistry = new ServiceRegistryBuilder().applySettings(
    configuration.getProperties()).buildServiceRegistry(); 
    sf = configuration.buildSessionFactory(serviceRegistry); 
    } catch (Throwable ex) { 
    System.err.println("Failed to create sessionFactory object." + ex); 
    throw new ExceptionInInitializerError(ex); 
    } 


    System.out.println("Hibernate Many to Many Mapping Example Using Xml "); 

    Session session = sf.openSession(); 
    session.beginTransaction(); 

    Student s1=new Student(1,"mina"); 
    Student s2=new Student(2,"samira"); 

    Course c1=new Course(10,"math"); 
    Course c2=new Course(11,"sport"); 

    s1.getcourse().add(c2); 
    s2.getcourse().add(c1); 
    s2.getcourse().add(c2); 

    session.save(s1); 
    session.save(s2); 

    session.getTransaction().commit(); 
    session.close(); 
    } 
} 

我的錯誤是:

Failed to create sessionFactory object.org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister 

Exception in thread "main" java.lang.ExceptionInInitializerError 
    at first.ManageStudent.main(ManageStudent.java:21) 
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister 
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:180) 
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:131) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:346) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) 
    at first.ManageStudent.main(ManageStudent.java:18) 
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer] 
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:138) 
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:188) 
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:336) 
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:498) 
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:142) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:158) 
    ... 4 more 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135) 
    ... 13 more 
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for courses in class first.Student 
    at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:316) 
    at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:310) 
    at org.hibernate.mapping.Property.getGetter(Property.java:298) 
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:436) 
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:200) 
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:82) 
    ... 18 more 

請幫幫我。 我的新的錯誤:

Failed to create sessionFactory object.org.hibernate.HibernateException: Wrong column type in SYSTEM.COURSE for column COURSE_NAME. Found: number, expected: varchar2(255 char) 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at first.ManageStudent.main(ManageStudent.java:21) 
Caused by: org.hibernate.HibernateException: Wrong column type in SYSTEM.COURSE for column COURSE_NAME. Found: number, expected: varchar2(255 char) 
    at org.hibernate.mapping.Table.validateColumns(Table.java:282) 
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1268) 
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:453) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) 
    at first.ManageStudent.main(ManageStudent.java:18) 
+0

將您的完整堆棧跟蹤 – mprabhat

回答

2

從代碼片段,我可以看到一點毛病:

  1. <class name="rStudent" table="STUDENT">當類名是學生,改變這一點,重新運行。

此異常通常是拋出當Hibernate犯規找到一個無參數的構造函數,或者你有一個循環依賴,因爲在你的代碼,我看到沒有參數的構造函數可用於初始化沒有循環依賴存在,糾正名稱應幫助解決這個問題。

按編輯:

你的getter,setter方法的課程是不正確的

而不是

public Set<Course> getcourse() // this should be getCourses 
{ 
    return courses; 
} 
public void setcourse(Set<Course> courses) // this should be setCourses 
{ 
    this.courses=courses; 
} 

類似的路線在Courses.java

public Set<Student> getstudents() // Make it getStudents 
{ 
    return students; 
} 
public void setstudents(Set<Student> students) // Make it setStudents 
{ 
    this.students=students; 
} 
請更改以下

你的getter,setter不遵循正確的命名約定紅色休眠。

檢查你的getter,setter方法爲學生與下面的代碼:

import java.util.HashSet; 
import java.util.Set; 

public class Student { 

    private int student_id; 
    private String student_name; 
    private Set<Course> courses = new HashSet<Course>(); 
    public Student() { 

    } 
    public Student(int id, String name) { 
     this.student_id = id; 
     this.student_name = name; 
    } 

    /** 
    * @return the student_id 
    */ 
    public int getStudent_id() { 
     return student_id; 
    } 

    /** 
    * @param student_id the student_id to set 
    */ 
    public void setStudent_id(int student_id) { 
     this.student_id = student_id; 
    } 

    /** 
    * @return the student_name 
    */ 
    public String getStudent_name() { 
     return student_name; 
    } 

    /** 
    * @param student_name the student_name to set 
    */ 
    public void setStudent_name(String student_name) { 
     this.student_name = student_name; 
    } 

    /** 
    * @return the courses 
    */ 
    public Set<Course> getCourses() { 
     return courses; 
    } 

    /** 
    * @param courses the courses to set 
    */ 
    public void setCourses(Set<Course> courses) { 
     this.courses = courses; 
    } 
} 

同樣,對於課程

import java.util.HashSet; 
import java.util.Set; 

public class Course { 
    private int   course_id; 
    private String  course_name; 
    private Set<Student> students = new HashSet<Student>(); 
    public Course() { 

    } 
    public Course(int id, String name) { 
     this.course_id = id; 
     this.course_name = name; 
    } 

    /** 
    * @return the course_id 
    */ 
    public int getCourse_id() { 
     return course_id; 
    } 

    /** 
    * @param course_id the course_id to set 
    */ 
    public void setCourse_id(int course_id) { 
     this.course_id = course_id; 
    } 

    /** 
    * @return the course_name 
    */ 
    public String getCourse_name() { 
     return course_name; 
    } 

    /** 
    * @param course_name the course_name to set 
    */ 
    public void setCourse_name(String course_name) { 
     this.course_name = course_name; 
    } 

    /** 
    * @return the students 
    */ 
    public Set<Student> getStudents() { 
     return students; 
    } 

    /** 
    * @param students the students to set 
    */ 
    public void setStudents(Set<Student> students) { 
     this.students = students; 
    } 
} 
+0

名稱正確。它是學生。但問題沒有解決。 – samira

+0

好的,你可以請給出完整的堆棧跟蹤.. – mprabhat

+0

你到底意味着什麼?我無法理解堆棧跟蹤。 – samira

1

有幾個問題我可以發現:

.hbm.xml有以下幾點: <set name="courses" …>name屬性的值必須是實體上屬性的名稱。然而,您的Student類只有方法getcourse()setcourse()。這就是堆棧跟蹤的原因:

找不到課程的第一個getter。學生

正確的方法名來定義屬性coursesgetCourses()setCourses()。這是對屬性重要的getter/setter方法名稱,而不是該字段的名稱。 (你應該糾正這個問題,對於你所有的獲得者和裝配者。)

另一件事是你的命名約定不遵循官方約定。在Java中,標準是camelCase字段和方法。雖然這對於領域並不重要,但在使用大量框架所依賴的JavaBean規範時,這是必要的。