开发者

Hibernate createQuery() throws ClassCastException, Transformer doesn't help

开发者 https://www.devze.com 2023-02-16 05:55 出处:网络
Trying to SELECT an entity using Using Hibernate 3.6 and MySQL5.1 but I keep getting a ClassCastException.

Trying to SELECT an entity using Using Hibernate 3.6 and MySQL5.1 but I keep getting a ClassCastException.

@Entity
@Table( name = "USER" )
public class User {
   @Id
   @Column(name= "user_id")
   private Long userId;

   @OneToOne()
   @JoinColumn(name="status_id")
   protected UserStatusType userStatusType;

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


@Entity
@Table( name = "REGION" )
public class Region   
   @Id
   @Column(name = "region_id")
   private Long regionId


@Entity
@Table( name = "USER_STATUS_TYPE" )
public class UserStatusType  
   @Id
   @Column(name = "type_id")
   private Long typeId

When attempting to use HQL in createQuery() I keep getting a ClassCastException:

session.beginTransaction();
Us开发者_JAVA百科er user = (User)session.createQuery(
   "from User u, UserStatusType ust, Region r "
   + " where u.userId = ? and u.userStatusType.typeId = ust.typeId"
   + " and u.region.regionId = r.regionId")
   .setLong(0, 42L)                     
   .uniqueResult();


 java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to com.scd.dao.entity.User

I tried using setResultTransformer(Transformers.aliasToBean(User.class)), but that gave me a NullPointerException.

Not sure what I'm doing wrong.


If FROM clause contains several entities, and you need only one of them, specify it in SELECT:

User user = (User)session.createQuery(
    "select u "
    + "from User u, UserStatusType ust, Region r "
    + " where u.userId = ? and u.userStatusType.typeId = ust.typeId"
    + " and u.region.regionId = r.regionId")
   .setLong(0, 42L)
   .uniqueResult(); 

Otherwise you'll get the tuple of entities as Object[].

If you actually need eager fetching instead of inner join, use JOIN FETCH:

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


The best way to do so is:

Query query = session.createQuery(queryString).addEntity(<Class Name>.class);
<Class Name> obj = (<Class Name>)query.uniqueResult();

Finally it worked for me try it...


try

session.beginTransaction();
User user = (User)session.createQuery(
   "select u from User u, UserStatusType ust, Region r "
   + " where u.userId = ? and u.userStatusType.typeId = ust.typeId"
   + " and u.region.regionId = r.regionId")
   .setLong(0, 42L)                     
   .uniqueResult();


You can solve this issue by adding .addEntity(User.class) in the end of the query. For example :

session.beginTransaction();
User user = (User)session.createQuery(
   "from User u, UserStatusType ust, Region r "
   + " where u.userId = ? and u.userStatusType.typeId = ust.typeId"
   + " and u.region.regionId = r.regionId")
   .setLong(0, 42L)                     
   .uniqueResult().addEntity(User.class);
0

精彩评论

暂无评论...
验证码 换一张
取 消