在hibernate中连接表的方法应该是什么

时间:2015-12-02 08:15:20

标签: java hibernate

我的POJO Student.java

package foo;

public class Student {
    private int student_id;
    private String name;
    private College college;

    Student(){}
    public Student(String name,College college){
        this.name=name;
        this.college=college;
    }



    public void setCollege(College college) {
        this.college = college;
    }
    public College getCollege() {
        return college;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setStudentId(int id) {
        this.student_id = id;
    }
    public int getStudentId() {
        return student_id;
    }

}

College.java

package foo;

public class College {
    private String college_code;
    private String name;
    private String city;

    College(){}
    public College(String college_code,String name ,String city){
        this.setCollegeCode(college_code);
        this.setName(name);
        this.setCity(city);

    }


    public void setCollegeCode(String college_code) {
        this.college_code = college_code;
    }
    public String getCollegeCode() {
        return college_code;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCity() {
        return city;
    }

}

我的映射文件。 的 College.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 Dec 2, 2015 11:44:19 AM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
    <class name="foo.College" table="college" catalog="Students">
        <id name="collegeCode" type="string">
            <column name="college_code" length="5" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="50" />
        </property>
        <property name="city" type="string">
            <column name="city" length="20" />
        </property>
    </class>
</hibernate-mapping>

Student.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 Dec 2, 2015 11:44:19 AM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
    <class name="foo.Student" table="student" catalog="Students">
        <id name="studentId" type="int">
            <column name="student_id" />
            <generator class="increment" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" />
        </property>

        <many-to-one name="college" column="college_code" 
       class="foo.College" not-null="true"/>
    </class>
</hibernate-mapping>

和主要课程

Main.java

package utils;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.sql.ordering.antlr.Factory;

import foo.College;
import foo.Student;

public class Main {
    public static SessionFactory factory;

    public static void main(String[] args) {


        try{
             factory = new Configuration().configure().buildSessionFactory();
          }catch (Throwable ex) { 
             System.err.println("Failed to create sessionFactory object." + ex);
             throw new ExceptionInInitializerError(ex); 
          }
          Main obj=new Main();
         //College clg1= obj.addCollege("00000","RKGIT","GHAZIABAD"); //Add a college to start with
          //College clg1=obj.listCollege("00000");
         //Integer std1= obj.addStudent("Vishal Tyagi",clg1); //add students
         //System.out.println(std1);
          obj.listStudents();

}
    //Add college
    private College addCollege(String code,String name,String city){
        Session session =factory.openSession();
        Transaction tx;
        tx=session.beginTransaction();

        College clg1=new College(code,name,city);
        session.save(clg1);
        tx.commit();
        return clg1;


    }

private Integer addStudent(String name,College college)
{
    Session session =factory.openSession();
    Transaction tx=session.beginTransaction();

    Student std1=new Student(name,college);
    Integer id1=(Integer)session.save(std1);
    tx.commit();

    return id1;

    }
private College listCollege(String code){
    Session session =factory.openSession();
    Transaction tx=session.beginTransaction();
    Query query =session.createQuery("from College where college_code=:code");
    query.setParameter("code", code);
    List list=query.list();
    if(!list.isEmpty())
        {
        for(Object a :list)
            return (College)a;
        }
    return null;
}

/*private void listStudents(){
    Session session =factory.openSession();
    Transaction tx=session.beginTransaction();
    String querystring="from Student";
    Query query=session.createQuery(querystring);
    List list= query.list();
    for(Object a:list)
    {
        Student student=(Student)a;
        System.out.println("Name : "+student.getName());
        System.out.println("ID : "+student.getStudentId());
        College college=student.getCollege();
        System.out.println("Collegecode :"+college.getCollegeCode());
        System.out.println("Collegename :"+college.getName());
        System.out.println("collegecity :" +
                ""+college.getCity());
        System.out.println("--------------------------------");

    }
}*/


private void listStudents(){
    Session session =factory.openSession();
    Transaction tx=session.beginTransaction();
    String querystring="from Student a,College b where a.college.getCollegeCode()=b.college_code";
    Query query=session.createQuery(querystring);
    List list=query.list();
    for(Object a :list){
        System.out.println(a.toString());
    }
    tx.commit();
 }
}

我可以通过注释的listStudents方法列出学生,但是,如何在Hibernate中将这两个表连接在一起?

此方法listStudents提供错误:&#34; 无法解析属性&#34;

我只是走错路,我的评论列表学生方法将满足我的目的吗?或者有没有办法加入这两个表学生和大学。

1 个答案:

答案 0 :(得分:1)

您在查询中使用的是getter而不是属性collegeCode。这是错误的