嘗試使用使用Hibernate 3.6和MySQL5.1選擇一個實體,但我不斷收到一個ClassCastException。Hibernate createQuery()拋出ClassCastException,Transformer沒有幫助
@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
當試圖在的createQuery()我一直得到一個ClassCastException使用HQL:
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();
java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to com.scd.dao.entity.User
我嘗試使用setResultTransformer(Transformers.aliasToBean(User.class)),但是這給了我一個NullPointerException 。
不知道我在做什麼錯。
你會好心接受你的問題的答案嗎?這就是它在stackoverflow上的工作原理。謝謝! – Falcon