hibernate 单向多对对的关联遇到的问题

J2EE 码拜 9年前 (2015-04-23) 1376次浏览 0个评论

是学生和课程之间的关联
studnet类

public class Student1 implements java.io.Serializable {

// Fields

private String studentId;
private String studentNumber;
private String studnetName;
    private Set course=new HashSet();
// Constructors

/** default constructor */
public Student1() {
}

/** full constructor */
public Student1(String studentId, String studentNumber, String studnetName) {
this.studentId = studentId;
this.studentNumber = studentNumber;
this.studnetName = studnetName;
}

// Property accessors

public String getStudentId() {
return this.studentId;
}

public void setStudentId(String studentId) {
this.studentId = studentId;
}

public String getStudentNumber() {
return this.studentNumber;
}

public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}

public String getStudnetName() {
return this.studnetName;
}

public void setStudnetName(String studnetName) {
this.studnetName = studnetName;
}

public Set getCourse() {
return this.course;
}

public void setCourse(Set course) {
this.course = course;
}

}
course类
public class Course implements java.io.Serializable {

// Fields

private String courseId;
private String courseName;

// Constructors

/** default constructor */
public Course() {
}

/** full constructor */
public Course(String courseId, String courseName) {
this.courseId = courseId;
this.courseName = courseName;
}

// Property accessors

public String getCourseId() {
return this.courseId;
}

public void setCourseId(String courseId) {
this.courseId = courseId;
}

public String getCourseName() {
return this.courseName;
}

public void setCourseName(String courseName) {
this.courseName = courseName;
}

}
其中中间表studnet_course
hibernate 单向多对对的关联遇到的问题
studnet1的配置文件
<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<!– 
    Mapping file autogenerated by MyEclipse Persistence Tools
–>
<hibernate-mapping>
    <class name=”model.Student1″ table=”student1″ catalog=”hib”>
        <id name=”studentId” type=”java.lang.String”>
            <column name=”STUDENT_ID” length=”20″ />
            <generator class=”assigned” />
        </id>
        <property name=”studentNumber” type=”java.lang.String”>
            <column name=”STUDENT_NUMBER” length=”20″ not-null=”true” />
        </property>
        <property name=”studnetName” type=”java.lang.String”>
            <column name=”STUDNET_NAME” length=”10″ not-null=”true” />
        </property>
        
       <!–  –> 
       <set name=”course” table=”STUDENT_COURSE” cascade=”all”>
       <key column=”STUDENT_ID”/>
       <many-to-many column=”COURSE_ID” class=”model.Course”></many-to-many>
       </set>
    </class>
</hibernate-mapping>
course配置文件
<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<!– 
    Mapping file autogenerated by MyEclipse Persistence Tools
–>
<hibernate-mapping>
    <class name=”model.Course” table=”course” catalog=”hib”>
        <id name=”courseId” type=”java.lang.String”>
            <column name=”COURSE_ID” length=”20″ />
            <generator class=”assigned” />
        </id>
        <property name=”courseName” type=”java.lang.String”>
            <column name=”COURSE_NAME” length=”20″ not-null=”true” />
        </property>
    </class>
</hibernate-mapping>

测试时
public static List get_course()
{
Session session=null;
Transaction transaction=null;
try {
session=HibernateSessionFactory.getSession();
transaction=session.beginTransaction();
Query query=session.createQuery(“from Student1 where studentId=?”);
query.setParameter(0, “123”);
List list=query.list();
transaction.commit();
return list;
} catch (Exception e) {
// TODO: handle exception
if(transaction!=null) transaction.rollback();
return null;
}
/*
finally
{
session.close();
}*/
}

@Test
public void test()
{
List list=StudentDAO.get_course();
for(int i=0;i<list.size();i++)
{
Student1 studnet=(Student1)list.get(i);
if(list!=null) System.out.println(“OK”);
System.out.println(“学生学号: “+studnet.getStudentId());
System.out.println(“学生姓名: “+studnet.getStudnetName());
Iterator iterator=studnet.getCourse().iterator();
while(iterator.hasNext())
{
String string=(String)iterator.next();
Course course=(Course)iterator.next();
System.out.println(“课程ID:”+course.getCourseId());
System.out.println(“课程名:”+course.getCourseName());
System.out.println(string);
}
System.out.println(“end”);
}
}

报错
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [model.Student1.course#123]。。。。。。。。。。。。。

hibernate 单向多对对的关联遇到的问题
坐等高手回复
hibernate 单向多对对的关联遇到的问题
10分
你的测试类没把配置文件加载进来吗?没openSession吗?
hibernate 单向多对对的关联遇到的问题
引用 2 楼 u014068144 的回复:

你的测试类没把配置文件加载进来吗?没openSession吗?

肯定有啊 !

hibernate 单向多对对的关联遇到的问题
10分
定义Set时候不要new HashSet()试试
刚学hibernate,不大会凑个热闹~

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明hibernate 单向多对对的关联遇到的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!