gpt4 book ai didi

java.lang.ClassCastException : [Ljava. lang.Object;不能转换到

转载 作者:行者123 更新时间:2023-12-01 14:09:17 26 4
gpt4 key购买 nike

为什么在我的程序中会触发 java.lang.ClassCastException ?

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.App.Equip]



查询返回已回答(在 CheckLists calsse 中找到)但尚未由 Equip 对象回答的 list 列表

- 这是代码:
import org.json.simple.*;

@SuppressWarnings("unchecked")

public JSONObject ListCheckListsNonETRepondu( long idEqp, long idmiss){

Query query = manager.createNativeQuery("SELECT"
+ " checksl.id_check_lists as IdCheckLists,"
+ " checksl.titre_check as NomCheckLists,"
+ " checksl.recommendation as Recommendation, "
+ " resp.id_responsescheck as IdResponse, "
+ " resp.conformite as Conformite, "
+ " resp.date_response as DateResponse, "
+ " resp.missions_id as IdMission "
+ " FROM equipements eq "
+ " LEFT JOIN check_lists checksl"
+ " ON eq.id_equipements= checksl.equipements_id "
+ " LEFT JOIN responses_check_lists resp "
+ " ON checksl.id_check_lists = resp.check_lists_id "
+ " AND resp.missions_id ="+idmiss+""
+ " AND eq.id_equipements ="+idEqp
+ " ORDER BY checksl.id_check_lists"
);

List<Equip> res = query.getResultList();

JSONObject obj = new JSONObject();

for( Equip eq: res) //--The problem is here --
{
for(CheckLists checks : eq.getChecks())
{
obj.put("idCheckLists", checks.getIdCheckLists());
obj.put("NomCheckLists", checks.getTitreCheck());
obj.put("Recommendation", checks.getRecommendation());

for(ResponsesCheckLists resp :checks.getResponsesChecks())
{
obj.put("IdResponse",resp.getIdResponsesCHeck());
obj.put("DateResponse",resp.getDateResponse());
obj.put("Conformite",resp.isConformite());
obj.put("IdMission",resp.getRespmission().getIdMission());
}
}

}
return (JSONObject)obj;
}

-我的java类:
@Entity
public class CheckLists implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idCheckLists")
private long idCheckLists;

@Column(name="titreCheck")
private String titreCheck;

@Column(name="recommendation")
private String recommendation;

@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="equipements_id")
@JsonBackReference
private Equipements equipements;

@OneToMany(mappedBy="CheckLts", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
//@Fetch(value = FetchMode.SUBSELECT)
private Set<ResponsesCheckLists> ResponsesChecks;
..
}

//
@Entity
public class ResponsesCheckLists implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idResponsesCHeck")
private long idResponsesCHeck;

@Column(name="conformite")
private boolean conformite;

@Column(name="dateResponse")
private String dateResponse;

@ManyToOne
@JoinColumn(name="missionsId")
private Missions Respmission;

@ManyToOne
@JoinColumn(name="checkLists_Id")
private CheckLists CheckLts;

....
}

//
@Entity
public class Equip implements Serializable{


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idEquipements")
private long idEquipements;

@Column(name="nomEq")
private String nomEq;

@Column(name="dateAjoutEq")
private String dateAjoutEq;

@Column(name="dateModificationEq")
private String dateModificationEq;

@OneToMany(mappedBy="equipements", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
//@Fetch(value = FetchMode.SUBSELECT)
@JsonManagedReference
private Set<CheckLists> checks;

@ManyToOne
@JoinColumn(name="actifs_id")
private Actifs actifsEquipements;
}

我想以 Json 格式格式化我的 SQl 查询的结果。
这是 SQL 查询返回的内容 query.getResultList() :
[
[
1,
"2.1 Create Separate Partition ",
"Description.... ",
1,
false,
"25/05/2017",
15
],
[
2,
"2.2 Set nodev option ",
" Description:.... ",
1,
false,
"25/05/2017",
15
]
......
]

谁能介意给我一些建议?

非常感谢!!!

最佳答案

您的查询返回 ListObject[]因为您没有选择 Equip实体,但您只选择 中的列:

   Query query = manager.createNativeQuery("SELECT"
+ " checksl.id_check_lists as IdCheckLists,"
+ " checksl.titre_check as NomCheckLists,"
+ " checksl.recommendation as Recommendation, "
+ " resp.id_responsescheck as IdResponse, "
+ " resp.conformite as Conformite, "
+ " resp.date_response as DateResponse, "
+ " resp.missions_id as IdMission "

Hibernate 不会转换 ResultSet结果为 Equip实体对象,结果将是 object 的数组s 因为 Hibernate 不会确定所选列的类型。

你需要遍历这个 List元素并转换每个 Object[]Equip手动对象。

编辑:

这是你应该如何实现它:
List<Object[]> res = query.getResultList();
List<Equip> list= new ArrayList<Equip>();
JSONObject obj = new JSONObject();

Iterator it = res.iterator();
while(it.hasNext()){
Object[] line = it.next();
Equip eq = new Equip();
eq.setIdEquipement(line[0]);
eq.setTitre(line[1]);
eq.setDescription(line[2]);
//And set all the Equip fields here
//And last thing add it to the list

list.add(eq);
}

关于java.lang.ClassCastException : [Ljava. lang.Object;不能转换到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285188/

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