How to get the return value of HQL? (Robin)

xiaoxiao2021-03-06  45

Many people don't know how to get the return value of HQL. The following example shows: Take a single field, return field list: query q = S.CREATEQUERY ("Select C.ID from cat as c");

List l = q.list ();

For (i = 0; i

Long id = (long) L.GET (i);

System.out.println (ID.longValue ());

}

Take multiple fields, Hibernate makes each ID and name a single-dimensional array of 2 elements, and the List is actually a collection of single-dimensional array. Query Q = S.CREATEQUERY ("Select Cat.ID, Cat.Name, from cat");

List l = q.list ();

For (int i = 0; i

Object [] row = (Object []) L.GET (i);

Long id = (long) row [0];

String name = (string) row [1];

}

In this case, HQL does not construct PO, this is easy to verify. JCS is object cache, if you write such HQL, JCS is empty, indicating that Hibernate does not construct PO, if you add an object C itself, the JCS has data, as follows: query q = s.createQuery (" Select C.ID, C.Name, C from Cat AS C ");

List l = q.list ();

For (int i = 0; i

Object [] row = (Object []) L.GET (i);

Long id = (long) row [0];

String name = (string) row [1];

Cat c = (cat) row [2];

}

转载请注明原文地址:https://www.9cbs.com/read-66571.html

New Post(0)