Returning multiple object types using hibernate using an inner join

前端 未结 3 1509
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 01:55

I seem to be having some difficulty with a query in hibernate. I am performing an inner join on two tables.

SELECT * FROM product p INNER JOIN warehouse w ON         


        
3条回答
  •  情歌与酒
    2020-12-30 02:29

    Reference

    In case the entities aren't from the same class, then here's a sample :

    public static void main(String[] args) {
            Session sess = NewHibernateUtil.getSessionFactory().openSession();
            SQLQuery q = null;
            String query = "select a.*, u.* from user u, account a where a.iduser=u.iduser";
            q = sess.createSQLQuery(query);
            q.addEntity(User.class);
            q.addEntity(Account.class);
            List lst = q.list();
            System.out.println("" + lst.size());
    
            for (int i = 0; i < lst.size(); i++) {
                System.out.println(((Object[]) lst.get(i))[0]);     //account bean, actually this is in reverse order - so this is user bean
                System.out.println(((Object[]) lst.get(i))[1]);     //user bean         & this account bean
            }
            sess.close();
        }
    

提交回复
热议问题