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!!!
To prevent the ClassCastException exception, one should be careful when casting objects to a specific class or interface and ensure that the target type is a child of the source type, and that the actual object is an instance of that type.
ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.
It's just what the toString method of a JVM array returns. The default implementation of toString that gets inherited from java.lang.Object is more or less equal to this: def toString(): String = this.getClass.getName + "@" + this.hashCode.toHexString. The class name of a String array is [Ljava. lang. String; .
Your query is returning a List
of Object[]
because you aren't selecting an Equip
Entity, but you are only selecting columns in :
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 won't convert the ResultSet
results to an Equip
entity object, the result will be an array of object
s because Hibernate won't determine the types of selected columns.
You need to loop over this List
elements and transform each Object[]
to an Equip
object manually.
Edit:
This is how you should implement it:
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With