简体   繁体   English

在休眠中联接表的方法应该是什么

[英]What should be the Approach for joining tables in hibernate

My POJO Student.java 我的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 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;
    }

}

My mapping files. 我的映射文件。 College.hbm.xml 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 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>

AND main class AND主类

Main.java 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();
 }
}

I am able to list students by the commented listStudents method but ,how do i join these two tables together in Hibernate? 我可以通过带注释的listStudents方法列出学生,但是,如何在Hibernate中将这两个表结合在一起?

This method listStudents gives Error:" Could not resolve property " 此方法listStudents给出错误:“ 无法解析属性

am i just going the wrong way and my Commented listStudents method will serve my purpose ?OR is there any way to join these two tables student and college. 我是不是走错了路,我的CommentedlistStudents方法将达到我的目的?或者有没有办法将这两个表加入学生和大学。

you are using the getter and not the property collegeCode in your query. 您正在使用getter,而不是查询中的属性collegeCode that is wrong 那是错的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM