Ok, I was able to get down to a single SELECT by enhancing the query, but now I'm getting a ClassCastException (see below).
Code:
Session session = currentSession();
session.beginTransaction();
User user = (User)session.createQuery(
"from User u, UserStatusType ust, UserRegion ur, " +
" where u.user_id = ? AND u.userStatusType.id = ust.id AND u.region.id = ur.id")
.setLong(0, 42L)
.uniqueResult();
Hibernate debug logging confirms that only a single SELECT statement was used, that's good, but I keep getting this cast exception and don't know what the heck is going on.
Code:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.scd.dao.entity.NewUser
at com.scd.entity.user.NewUserTest.selectNewUser(NewUserTest.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
I tried using setResultTransformer() but that resulted in a NullPointerException
Code:
User user = (User)session.createQuery(
"from User u, UserStatusType ust, UserRegion ur, " +
" where u.user_id = ? AND u.userStatusType.id = ust.id AND u.region.id = ur.id")
.setLong(0, 42L)
.setResultTransformer( Transformers.aliasToBean(User.class))
.uniqueResult();
Code:
java.lang.NullPointerException
at org.hibernate.transform.AliasToBeanResultTransformer.initialize(AliasToBeanResultTransformer.java:112)
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:81)
at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:96)
at org.hibernate.loader.hql.QueryLoader.getResultList(QueryLoader.java:423)