gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:38:26 34 4
gpt4 key购买 nike

我想在 hibernate 状态下查询两个表。用户实体中的 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'";

并运行查询:

  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();
}
}

用户实体类:这个类是 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 的 hibernate 映射

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

和用于创建 session 的 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;

}
}

最佳答案

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

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27280818/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com