javax.ejb.EJBException:java.lang.ClassCastException抛出(HQL查询)

javax.ejb.EJBException:java.lang.ClassCastException抛出(HQL查询),java,sql,jakarta-ee,ejb,Java,Sql,Jakarta Ee,Ejb,从EJB中查询数据库时遇到问题。代码如下: @Override public boolean register(User user) { // We have servlets that validate these fields, so we don't validate anything here // We check to see whether there is already a user with the same user name or

从EJB中查询数据库时遇到问题。代码如下:

@Override
    public boolean register(User user) {
        // We have servlets that validate these fields, so we don't validate anything here
        // We check to see whether there is already a user with the same user name or not
        Query query = (Query) entityManager.createQuery("SELECT u FROM User u WHERE u.username=:userName"); // Exception is thrown at this point
        ((javax.persistence.Query) query).setParameter("userName", user.getUsername());
        // We check each returned value. If we find anything than we do not add the user
        List<User> users = castList(User.class, ((javax.persistence.Query) query).getResultList());
        if(users.isEmpty()) {
            // The user is not present. Add this user
            entityManager.persist(user);
            // return "Welcome to BooksPoint " + user.getUsername() + "!";
            return true;
        } else {
            // return "Cannot create " + user.getUsername() + ". A user with this name already exists";
            return false;
        }

    }
删除(查询)将导致错误:

Type mismatch: cannot convert from javax.persistence.Query to javax.management.Query

如何强制转换查询对象?

检查类中的导入,显然您正在导入
javax.management.Query
您应该导入
javax.persistence.Query
哦,亲爱的。我认为您导入了错误的类文件进行查询。请检查您的导入,或者最好将完整的代码放在这里。检查您是否导入了
javax.management.Query
,而不是
javax.persistence.Query

您导入的类文件不正确,否则不需要在第3行中提供类文件的完整路径:-((javax.persistence.Query)查询).setParameter(“用户名”,user.getUsername());+1是的,我现在看到了:))这就是我信任eclipse的原因。我的eclipse总是为列表接口导入java.awt.List
Type mismatch: cannot convert from javax.persistence.Query to javax.management.Query