关于网友提出的“ hibernate 单向多对对的关联遇到的问题(困扰我很久了)”问题疑问,本网通过在网上对“ hibernate 单向多对对的关联遇到的问题(困扰我很久了)”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: hibernate 单向多对对的关联遇到的问题(困扰我很久了)描述:
hibernatemyeclipse
是学生和课程之间的关联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

studnet1的配置文件
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
course配置文件
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
测试时
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<>
{
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]。。。。。。。。。。。。。
解决方案1:
你的测试类没把配置文件加载进来吗?没openSession吗?
解决方案2: 定义Set时候不要new HashSet()试试
刚学hibernate,不大会凑个热闹~