2014-12-03 55 views
7

我希望在休眠中查詢兩個表。 用戶實體中的featch 3表(用戶 - 角色 - 配置文件)。 查詢與HQL:java.lang.ClassCastException:[Ljava.lang.Object;不能投到entity.UserEntity

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'"; 

和運行查詢:

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類: 這個類是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; 

休眠馬平的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> 

和類HibernateUtil作爲創建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; 

     } 
    } 
+0

它將返回一個列表,它將包含您的數據。如果您想要返回UserEntity,那麼只需使用「從UserEntity ue中選擇ue,RoleEntity重新獲取所有屬性,其中ue.roleEntity.roleId = re.roleId和ue.username ='reza'並且ue.password ='123456'」 – mprabhat 2014-12-03 20:22:35

+0

爲什麼要捕獲一個HibernateException並且除了拋出一個拋出引發異常的DaoException之外什麼都不做? catch(HibernateException e)嘗試throw new DaoException(「Fetal exception in」,e); (DaoException e1){ e1.printStackTrace(); } }' – Sal 2014-12-03 23:09:19

+0

嗨mprabhat。我希望角色和用戶的所有數據表都可以得到。 – 2014-12-04 05:48:15

回答

12

因爲使用多選擇投影時,實際上是獲取對象的陣列,所以需要將查詢結果處理邏輯更改爲:

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

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