java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 entity.UserEntity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27280818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:05:57  来源:igfitidea点击:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to entity.UserEntity

javahibernatejakarta-eeormhql

提问by reza rostami

i want query two table in hibernate . featch 3 table (User - Role - Profile ) in user Entity . query with hql :

我想在休眠中查询两个表。用户实体中的 featch 3 表(用户-角色-配置文件)。使用 hql 查询:

query= "select ue, ue.roleEntity.roleId from UserEntity ue ,RoleEntity re  fetch all properties where ue.roleEntity.roleId=re.roleId and ue.username ='reza' and ue.password='123456'";

and run query :

并运行查询:

  try {
        sessionFactory = HibernateUtil.getSessionFactory();
        session = sessionFactory.getCurrentSession();
        transaction = session.beginTransaction();
        userEntityList = (List<UserEntity>) session.createQuery(query).list();
        transaction.commit();
    } catch (HibernateException e) {
        try {
            throw new DaoException("Fetal exception in", e);
        } catch (DaoException e1) {
            e1.printStackTrace();
        }
    }

userentity class : this class is geteer and seter :

用户实体类:这个类是 geteer 和 seter :

public class UserEntity {
private int userId;
private long personalCode;
private String username;
private String password;
private short active;
private String question;
private String passive;
private ProfileEntity  profileEntity;
private RoleEntity roleEntity;

hibernate maping for userEntity.hbm.xml

userEntity.hbm.xml 的休眠映射

        <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping package="entity">
        <class name="UserEntity" table="TABLE_USER">
            <id name="userId" type="java.lang.Integer" column="USER_ID">
                <generator class="increment" />
            </id>

            <property name="personalCode" type="java.lang.Long" column="PERSONALCODE">

            </property>

            <property name="username" type="java.lang.String" column="USERNAME">

            </property>

            <property name="password" type="java.lang.String" column="PASSWORD">

            </property>

            <property name="active" type="java.lang.Short" column="ACTIVE">

            </property>

            <property name="question" type="java.lang.String" column="QUCTION">

            </property>

            <property name="passive" type="java.lang.String" column="PASSIVE">

            </property>


            <many-to-one name="roleEntity" class="entity.RoleEntity" column="ROLE_ID" cascade="none" fetch="select" />
            <many-to-one name="profileEntity" class="ProfileEntity" cascade="delete" column="profile_id"/>

    </class>
</hibernate-mapping>

and class hibernateutil for create sesstion :

和类 hibernateutil 用于创建会话:

  import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;

    public class HibernateUtil {
        private static SessionFactory sessionFactory;

        static {
            try {
                Configuration configuration = new Configuration().configure();
                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(builder.build());
            } catch (Throwable th) {

                System.err.println("Enitial SessionFactory creation failed" + th);

                throw new ExceptionInInitializerError(th);

            }

        }

        /**
         * @return
         */
        public static SessionFactory getSessionFactory() {

            return sessionFactory;

        }
    }

采纳答案by Vlad Mihalcea

Because you are using a multi-selection projection, you are actually fetching an array of objects, so you need to change the query result processing logic to:

因为你使用的是多选投影,你实际上是在获取一个对象数组,所以需要将查询结果处理逻辑改为:

List<Object[]> tuples = (List<Object[]>) session.createQuery(query).list();

for(Object[] tuple : tuples) {
    UserEntity ue = tuple[0];
    Number roleId = tuple[1];
}