5

When I have the below query it will give me a list of Product.

List<Product>=

getCurrentSession().createQuery("SELECT p FROM Product p ").list();

What will it return when there is a join as below.

getCurrentSession().createQuery("SELECT p FROM Product p inner join ProductCategory pc where p.id=pc.id").list();
Sanjaya Liyanage
  • 4,366
  • 9
  • 33
  • 50

5 Answers5

4

SELECT p FROM Product p inner join ... something like that gives you a list of Products.

FROM Product p inner join ... something like that gives you a list of arrays.

Bhesh Gurung
  • 48,464
  • 20
  • 87
  • 139
4

It should return List<Object[]> as a result. Please see this thread

And you should access your entities like

for (Object[]> result : query.list()) {
    Product p = (Product) result[0];
    ProductCategory pc = (ProductCategory) result[1];
}
Community
  • 1
  • 1
sanbhat
  • 16,864
  • 6
  • 46
  • 62
1

This will return a list of objects. You will have to cast them in to Product

List list = session.createQuery("SELECT p FROM Product p inner 
                      join ProductCategory pc where p.id=pc.id").list();
phenom
  • 3
  • 1
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
0

According to the documentation for javax.persistence.Query, you are going to get a List back. Why would you think it should be different?

hd1
  • 30,506
  • 4
  • 69
  • 81
0

This class casting from java.lang.object to model class doesn't work in any ways. Below is my code.

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

But In hibernate, you don't need join if you have both Entity associated with by @Many-to-one or @One-to-one join annotation. Then you need to select one object only , you will automatically get access to other object via the join. Make sure you keep getter(), setter() method for the joining field. Hope this will clarify the situation.

tinlyx
  • 18,900
  • 26
  • 81
  • 148