Java Hibernate异常:ClassCastException和QueryException
我想要的是一个客户可以访问其个人资料页面的页面。他需要点击的锚如下所示:Java Hibernate异常:ClassCastException和QueryException,java,hibernate,classcastexception,Java,Hibernate,Classcastexception,我想要的是一个客户可以访问其个人资料页面的页面。他需要点击的锚如下所示: <a href="${pageContext.request.contextPath}/customer/profile/${pageContext.request.userPrincipal.name}"></a> 接下来我有一个用户模型和一个客户模型。为了最小化代码,我省略了所有的构造函数和一些getter和setter 用户模型 @Entity @Table(name = "user", c
<a href="${pageContext.request.contextPath}/customer/profile/${pageContext.request.userPrincipal.name}"></a>
接下来我有一个用户模型和一个客户模型。为了最小化代码,我省略了所有的构造函数和一些getter和setter
用户模型
@Entity
@Table(name = "user", catalog = "...")
public class User implements java.io.Serializable {
private String username;
private boolean enabled;
private String password;
private Set<Storeproperties> storepropertieses = new HashSet<Storeproperties>(
0);
private Set<UserRole> userRoles = new HashSet<UserRole>(0);
private Set<Customerproperties> customerpropertieses = new HashSet<Customerproperties>(
0);
@Id
@Column(name = "username", unique = true, nullable = false, length = 45)
public String getUsername() {
return this.username;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<Customerproperties> getCustomerpropertieses() {
return this.customerpropertieses;
}
}
最后,我在DAO中使用的获取CustomerProperty的方法如下:
public Customerproperties getCustomerByUserName(String username) {
Customerproperties customer = (Customerproperties) getCurrentSession()
.createQuery("from Customerproperties c inner join c.User u where u.username = :user")
.setParameter("user", username)
.uniqueResult();
return customer;
}
当我单击链接时,会出现以下异常:
"HTTP Status 500 - Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: User of: com.IS202_1.users.model.Customerproperties [from com.IS202_1.users.model.Customerproperties c inner join c.User u where u.username = :user]"
"HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.IS202_1.users.model.Customerproperties"
当我将c.User更改为c.User时,我得到以下异常:
"HTTP Status 500 - Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: User of: com.IS202_1.users.model.Customerproperties [from com.IS202_1.users.model.Customerproperties c inner join c.User u where u.username = :user]"
"HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.IS202_1.users.model.Customerproperties"
为了更好地阅读,我建议将.setParameter(“用户”,用户名)更改为.setParameter(“用户名”,用户名)@realUser404,谢谢!您向我们展示了非常广泛的代码。试着把代码总结成一个最小的可复制的例子(见:)@Christophe好吧,我会试试的!