I have the following the classes:
@Entity
public class Equipements implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long idEquipements;
@OneToMany(mappedBy="equipements", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Collection<CheckLists> checks;
}
@Entity
public class CheckLists implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long idCheckLists;
private String titreCheck;
private String recommendation;
@ManyToOne
@JoinColumn(name="equipements_id")
private Equipements equipements;
@OneToMany(mappedBy="CheckLts", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Collection <ResponsesCheckLists> ResponsesChecks;
}
@Entity
public class ResponsesCheckLists implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long idResponsesCHeck;
@ManyToOne
@JoinColumn(name="missionsId")
private Missions Respmission;
@ManyToOne
@JoinColumn(name="checkLists_Id")
private CheckLists CheckLts;
}
I'm trying to show the list of checkLists that are answered (the answer is in the "ResponsesCheckLists" class) and not answered yet (the rest of the list is in the "checkLists" class).
I have the following The SQL query:
SELECT * FROM equipements eq
LEFT JOIN check_lists checks
ON eq.id_equipements = checks.equipements_id
LEFT JOIN responses_check_lists resp
ON checks.id_check_lists = resp.check_lists_id
AND resp.missions_id = 15
AND eq.id_equipements = 1
ORDER BY checks.id_check_lists
I followed the documents on the internet but I do not get the correct transformation of my query in JPQL.
The following query displays only the checkLists that are answered in the class ResponsesCheckLists:
@Query(
"SELECT checks, r "
+ " FROM CheckLists checks "
+ " inner join checks.ResponsesChecks r"
+ " WHERE checks.equipements.idEquipements = :idEqp "
+ " AND r.Respmission.idMission= :idmiss "
+ " ORDER BY checks.idCheckLists ASC"
)
I know that the attributes of the query will be replaced by the attributes of the class. But how to join three classes at the same time.
Edit1:
@Query(
"SELECT c, r "
+" FROM CheckLists c"
+" LEFT JOIN c.ResponsesChecks r"
+" JOIN fetch c.equipements e"
+" JOIN fetch r.Respmission m"
+" WHERE e.idEquipements = :idEqp"
+" AND m.idMission= :idmiss"
+" ORDER BY c.idCheckLists ASC"
)
This request does not display all the CheckLists of the "CheckLists" class and the checkLists that are answered (found in the "ResponsesCheckLists" class).
Edit2:
@Query(
"SELECT e "
+" FROM Equipements e"
+" LEFT JOIN fetch e.checks c "
+" LEFT JOIN fetch c.ResponsesChecks r"
+ " where e.idEquipements = :idEqp "
+ " and r.Respmission.idMission = :idmiss "
+ " order by c.idCheckLists asc"
)
Error of edit 2:
1- Servlet.service() for servlet [dispatcherServlet] in context with path [/App] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError)
2-WARN 3456 --- [nio-8099-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exceptionjava.lang.IllegalStateException: Cannot call sendError() after the response has been committed
Solution for error Edit2;
Add annotation @JsonIgnore
@JsonIgnore
Public Set <CheckLists> getChecks ()
{
Return checks;
}
-> After the implementation of this solution the request (edit2) does not display the list of checklists of a Equipments.
Edit3:
@Query(
"SELECT e "
+" FROM Equipements e , CheckLists c"
//+" LEFT JOIN fetch e.checks c "
+" LEFT JOIN c.ResponsesChecks r"
+ " where e.idEquipements = :idEqp "
+ " and r.Respmission.idMission = :idmiss "
+ " order by c.idCheckLists asc"
)
Resultat of Edit3:
[
{
"idEquipements": 1,
"nomEq": "Info",
"dateAjoutEq": "2017/04/09 20:37:58",
"dateModificationEq": "2017/04/09 20:48:40",
"checks": [] //133 items
},
{
"idEquipements": 1,
"nomEq": "Info",
"dateAjoutEq": "2017/04/09 20:37:58",
"dateModificationEq": "2017/04/09 20:48:40",
"checks": []//133 items
}
-I made a JPQL query to display list of checkLists that are answered (the answer is in the "ResponsesCheckLists" class) and not answered yet (the rest of the list is in the "checkLists" class). But my test shows a doubling of the Equipment object.
Thank you;