如何连接两个表并在hibernate中迭代

时间:2016-07-28 06:56:17

标签: mysql hibernate

如何编写hibernate查询以获取下面列的列表,这些列在jdbc中执行的mysql查询中执行。如何在我的Main类中编写HQL查询请告诉我

家长实体

 @Entity
@Table(name = "parent_info")
public class ParentDTO {
    @Id
    @GenericGenerator(name = "j", strategy = "increment")
    @GeneratedValue(generator = "j")
    @Column(name = "P_ID")
    private int p_id;

    @Column(name = "P_NAME")
    private String p_name;

    @Column(name = "P_PHONE")
    private String p_phone;

    @Column(name = "P_EMAIL")
    private String p_email;

    @Column(name = "REF_ID")
    private String ref_id;

    @OneToMany(cascade={CascadeType.ALL})
    @JoinColumn(name="student_id")

    private List<StudentDTO> students;


    public List<StudentDTO> getStudents() {
        return students;
    }

    public void setStudents(List<StudentDTO> students) {
        this.students = students;
    }

    public int getP_id() {
        return p_id;
    }

    public void setP_id(int p_id) {
        this.p_id = p_id;
    }

    public String getP_name() {
        return p_name;
    }

    public void setP_name(String p_name) {
        this.p_name = p_name;
    }

    public String getP_phone() {
        return p_phone;
    }

    public void setP_phone(String p_phone) {
        this.p_phone = p_phone;
    }

    public String getP_email() {
        return p_email;
    }

    public void setP_email(String p_email) {
        this.p_email = p_email;
    }

    public String getRef_id() {
        return ref_id;
    }

    public void setRef_id(String ref_id) {
        this.ref_id = ref_id;
    }


}

学生实体班

 @Entity
@Table(name = "student_info")
public class StudentDTO {
    @Id
    @GenericGenerator(name = "j", strategy = "increment")
    @GeneratedValue(generator = "j")
    @Column(name = "S_ID")
    private int s_id;

    @Column(name = "S_NAME")
    private String s_name;

    @Column(name = "S_PHONE")
    private String s_phone;

    @Column(name = "S_EMAIL")
    private String s_email;

    @Column(name = "REF_ID")
    private String ref_id;

    @Column(name = "S_CLASS_NAME")
    private String s_class_name;



    @ManyToOne
    @JoinColumn(name="parent_id")
    private ParentDTO parent;




    public ParentDTO getParent() {
        return parent;
    }

    public void setParent(ParentDTO parent) {
        this.parent = parent;
    }

    public int getS_id() {
        return s_id;
    }

    public void setS_id(int s_id) {
        this.s_id = s_id;
    }

    public String getS_name() {
        return s_name;
    }

    public void setS_name(String s_name) {
        this.s_name = s_name;
    }

    public String getS_phone() {
        return s_phone;
    }

    public void setS_phone(String s_phone) {
        this.s_phone = s_phone;
    }

    public String getS_email() {
        return s_email;
    }

    public void setS_email(String s_email) {
        this.s_email = s_email;
    }

    public String getRef_id() {
        return ref_id;
    }

    public void setRef_id(String ref_id) {
        this.ref_id = ref_id;
    }

    public String getS_class_name() {
        return s_class_name;
    }

    public void setS_class_name(String s_class_name) {
        this.s_class_name = s_class_name;
    }

}

主要课程

public class Test {

    public static void main(String[] args) {
        Session session = null;
        Transaction tx = null;
        List<StudentDTO> groupList = null;
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();


            session.getTransaction().commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            session.close();
        }
    }
}

我的SQL查询在jdbc中执行

选择pt.P_MOBILE,pt.P_EMAIL,st.S_FIRSTNAME,st.REF_ID来自parent_info pt join student_info st on pt.REF_ID = st.REF_ID where st.S_CLASS_TO_JOIN = 10;

1 个答案:

答案 0 :(得分:0)

String query="paste your query here ";
List<Object[]> objects = session.createSQLQuery(query).list();
                ListIterator<Object[]> iterator = objects.listIterator();
                while (iterator.hasNext()) {
                    Object[] object = (Object[]) iterator.next();

                    int firstcolumn=(Integer) object[0];
                    int secondcolumn=(Integer) object[1];

                    }