-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JOIN not working; too many SELECTs querying single object
PostPosted: Sat Mar 12, 2011 12:12 pm 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
The problem I've got is that Hibernate executes 3 SELECT statements when I query for a single, unique entity. I suspect my query and/or annotation are not correct.

A User has a UserStatusType and Region; simple one-to-one mappings.

Code:
@Entity
@Table( name = "USER" )
public class User extends {

   @Id
   @Column(name= "user_id")
   private Long userId;
   
   @OneToOne()
   @JoinColumn(name="user_status_type_id")
   protected UserStatusType userStatusType;

   @OneToOne()
   @JoinColumn(name="region_id")
   protected Region region;


My query code:

Code:
   Session session = currentSession();
   session.beginTransaction();
   User user = (User)session.createQuery(
      "from User as user where user_id = ?")
      .setLong(0, 42L)
      .uniqueResult();


Top
 Profile  
 
 Post subject: Re: JOIN not working; too many SELECTs querying single object
PostPosted: Sat Mar 12, 2011 2:51 pm 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
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)


Top
 Profile  
 
 Post subject: Re: JOIN not working; too many SELECTs querying single object
PostPosted: Sat Mar 12, 2011 8:35 pm 
Newbie

Joined: Tue Feb 01, 2011 2:06 am
Posts: 8
Ok, I figured this out, looks like I'm answering my own question, I hope it helps those reading this in the future.

I eliminated the ClassCastException by adding "Select u" to the query; this informs Hibernate to return that entity type; otherwise, an Object[] array is returned and that's what caused the cast error. I also used explicit "fetching" in the query to reduce the database operation to a single SELECT.

Code:
User user = (User)session.createQuery(
      "Select u from User u " +
      " join FETCH u.userStatusType " +
      " join FETCH u.region " +
      " where u.userId = ?")
      .setLong(0, 42L)
      .uniqueResult();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.