Hibernate: How to get result from query with multiple classes
for (Object[] result : query.list()) { User user = (User) result[0]; Group group = (Group) result[1]; }
for (Object[] result : query.list()) { User user = (User) result[0]; Group group = (Group) result[1]; }
The straightforward version of this query looks like this (assuming that (name, version) pairs are unique): select f from FeatureList f where f.version = (select max(ff.version) from FeatureList ff where ff.name = f.name)
Shouldn’t it be current_date? Hibernate will translate it to the proper dialect. I did not find a real “Hibernate will translate this to that” reference documentation, but the expression, in general, can be found in HQL Expressions for Hibernate 4.3. Then there is the Java Persistence API 2.0 (JPA) specification which defines expressions for the … Read more
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource Problem is (List<SwitcherServiceSource>) LoadSource.list(); This will return a List of Object arrays (Object[]) with scalar values for each column in the SwitcherServiceSource table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values. Solution List<Object> result = (List<Object>) LoadSource.list(); Iterator itr = … Read more
As mentioned in a different answer to the question referenced previously, the following HQL construct works for me: select o from Product o WHERE :value is null or o.category = :value if :value is passed in as null, all Products are returned. See also Optional or Null Parameters Note that this won’t work in some … Read more
The equivalent to the nvl command in HQL is the coalesce command. coalesce(a,b) will return a if a is not null, otherwise b. So you would want something on the lines of: from Table where col1 = coalesce(:par1, ‘asdf’)
If you are using JPA 2.1 (Hibernate 4.3+) you can achieve what you want with @NamedEntityGraph. Basically, you would annotate your entity like this: @Entity @NamedEntityGraph(name = “Persons.noAddress”) public class Person { @Column private String name; @OneToMany(fetch=FetchType.EAGER) private List<String> address; } And then use the hints to fetch Person without address, like this: EntityGraph graph … Read more
Your named query is not valid (school_id is not a property of the Student entity), which prevents the SessionFactory from being instantiated. You need to think object and associations, not columns. Try this instead: from School as s where not exists ( from Student as st where st.school = s and st.status.id not in (0,1,2,3,4) … Read more
Using IS EMPTY should work (I would favor a JPQL syntax): SELECT u FROM User u WHERE u.status = 1 AND u.appointments IS EMPTY If it doesn’t, please show the generated SQL. References Hibernate Core Reference Guide 14.10. Expressions JPA 1.0 specification Section 4.6.11 “Empty Collection Comparison Expressions”
Parameters inside string literals are not resolved. You need to add %s to parameter values with string concatenation – either at the program side String QUERY = “FROM Person as p WHERE p.createUser = : createUser AND p.personId in ” + “(SELECT pn.personId FROM PersonName pn ” + “WHERE pn.personNameType=”FIRST” ” + “AND pn.name LIKE … Read more