why java.lang.ClassCastException is triggered in my program ?
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to
com.App.Equip]
The query returns the list of checklists that are answered (found in the CheckLists calsse) and not yet answered by Equip object
-Here is the code:
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;
}
-My java classes:
@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;
}
I want to format the result of my SQl query in Json format.
Here is what the SQL query returns 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
]
......
]
could anyone mind giving some advice for me?
Thanks a lot!!!
See Question&Answers more detail:
os