Java hibernate查询联接中返回的对象类型是什么

Java hibernate查询联接中返回的对象类型是什么,java,hibernate,Java,Hibernate,当我有下面的查询,它会给我一个产品列表 List<Product>= getCurrentSession().createQuery("SELECT p FROM Product p ").list(); List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>(); //listStudentInf

当我有下面的查询,它会给我一个产品列表

List<Product>=

getCurrentSession().createQuery("SELECT p FROM Product p ").list();
List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);

这将返回对象的
列表。您必须将它们转换到
产品中

List list = session.createQuery("SELECT p FROM Product p inner 
                      join ProductCategory pc where p.id=pc.id").list();
List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);

从产品p内部连接中选择p…
类似的内容将为您提供产品的列表

List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);

来自产品p内部连接…
类似的内容会为您提供一个数组列表。

根据javax.persistence.Query的文档,您将获得一个列表。为什么您会认为它应该有所不同?

因此它应该返回
列表
。请看线

List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);
您应该访问您的实体,如

for (Object[]> result : query.list()) {
    Product p = (Product) result[0];
    ProductCategory pc = (ProductCategory) result[1];
}
List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);

这个从java.lang.object到模型类的类转换在任何方面都不起作用。下面是我的代码

List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);
List listStudentInfo=new ArrayList();
//listStudentInfo=dataService.getAllStudent(studentInfo);
listStudentInfo=dataService.getStudentInfo();
System.out.println(listStudentInfo.size());
Contactinfo Contactinfo=(Contactinfo)listStudentInfo.get(0);

但在hibernate中,如果两个实体都由@Many-to-one或@one-to-one连接注释关联,则不需要连接。然后只需选择一个对象,即可通过连接自动访问其他对象。确保为联接字段保留getter()、setter()方法。希望这能澄清问题。

您将收到该列表。为什么不亲自尝试一下,看看它会带来什么回报?这真的有那么难吗?你能告诉我原因吗?两者都在连接表,但第一个表明确指定只返回产品。
List<Contactinfo> listStudentInfo = new ArrayList<Contactinfo>();                    
                         //listStudentInfo = dataService.getAllStudent(studentInfo);
                         listStudentInfo = dataService.getStudentInfo();
                         System.out.println(listStudentInfo.size()); 
                         Contactinfo contactinfo = (Contactinfo)listStudentInfo.get(0);