Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jpa - java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...