java.lang.ClassCastException:[Ljava.lang.Object;不能转换为entity.UserEntity [英] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to entity.UserEntity

查看:147
本文介绍了java.lang.ClassCastException:[Ljava.lang.Object;不能转换为entity.UserEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  query =从UserEntity中选择ue,ue.roleEntity.roleIe ue,RoleEntity re fetch所有属性,其中ue.roleEntity.roleId = re.roleId和ue.username ='reza'和ue.password ='123456'; 

并运行查询:

  try {
sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
userEntityList =(List< UserEntity>)session.createQuery(query).list();
transaction.commit();
catch(HibernateException e){
try {
抛出新的DaoException(e中的胎儿异常);
} catch(DaoException e1){
e1.printStackTrace();






$ p $ useType class:
这个类是geteer和seter:

  public class UserEntity {
private int userId;
私人长期personalCode;
私人字符串用户名;
私人字符串密码;
私人短期活跃;
私人字符串问题;
private String被动;
私人档案实体档案实体;
private RoleEntity roleEntity;



hibernate map for userEntity.hbm.xml


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

< hibernate-mapping package =entity>
< class name =UserEntitytable =TABLE_USER>
< id name =userIdtype =java.lang.Integercolumn =USER_ID>
< generator class =increment/>
< / id>

< property name =personalCodetype =java.lang.Longcolumn =PERSONALCODE>

< / property>

< property name =usernametype =java.lang.Stringcolumn =USERNAME>

< / property>

< property name =passwordtype =java.lang.Stringcolumn =PASSWORD>

< / property>


< / property>

< property name =questiontype =java.lang.Stringcolumn =QUCTION>

< / property>


< / property>


<多对一名称=roleEntityclass =entity.RoleEntitycolumn =ROLE_IDcascade =nonefetch =select/>

< / class>
< / hibernate-mapping>

和类hibernateutil用于创建操作:

  import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
导入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);

抛出新的ExceptionInInitializerError(th);




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

return sessionFactory;


$ b


解决方案

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

 列表与LT;对象[]> tuples =(List< Object []>)session.createQuery(query).list(); 

for(Object [] tuple:tuples){
UserEntity ue = tuple [0];
数字roleId =元组[1];
}


i want query two table in hibernate . featch 3 table (User - Role - Profile ) in user Entity . query with 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 :

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

        <?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 :

  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;

        }
    }

解决方案

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];
}

这篇关于java.lang.ClassCastException:[Ljava.lang.Object;不能转换为entity.UserEntity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆