Hibernate Exceptions:ClassCastException和QueryException

时间:2014-12-01 22:15:47

标签: java hibernate classcastexception

我想要的是客户可以访问他的个人资料页面。他需要点击的锚点如下:

<a href="${pageContext.request.contextPath}/customer/profile/${pageContext.request.userPrincipal.name}"></a>

我的控制器中有以下方法:

@RequestMapping(value = "/profile/{username}", method = RequestMethod.GET)
    public ModelAndView editProfileIdPage(@PathVariable("username") String username) {

        ModelAndView customerEditView = new ModelAndView("/customer/customerHome");
        customerEditView.addObject("customer", customerService.getCustomerByUserName(username));
        customerEditView.addObject("title", "Welkom");

        return customerEditView;
    }

接下来我有一个User模型和一个Customer模型。我遗漏了所有构造函数和一些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;
        }

    }

CUSTOMER MODEL:

@Entity
@Table(name = "customerproperties", catalog = "...")
public class Customerproperties implements java.io.Serializable {

    private int customerid;
    private User user;
    private String address;
    private String city;
    private String phonenumber;
    private String zipcode;
    private String email;

    @Id
    @GeneratedValue(strategy = AUTO)
    @Column(name = "customerid", unique = true, nullable = false)
    public int getCustomerid() {
        return customerid;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_username", nullable = false)
    public User getUser() {
        return this.user;
    }

}

最后,我在DAO中使用的获取customerproperties的方法如下:

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]"

当我将c.User更改为c.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" 

0 个答案:

没有答案