0
votes
@Override
public List<Projet> getScenariosByProjet(int idProjet) {
    Session session =  HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    String sql = "select * from projet where scenario="+idProjet;
    Query query = session.createSQLQuery(sql);
    List<Projet> scenarios = (List<Projet>) query.list();
    return scenarios;
}

the class Projet :

public class Projet {

private int idProjet;
private String nomProjet;
private Set<Projet> scenarios = new HashSet<Projet>();
//.....

the hbm file :

<hibernate-mapping>
<class name="com.model.Projet" table="PROJET">
    <id name="idProjet" type="int">
        <column name="IDPROJET" />
        <generator class="increment" />
    </id>
    <property name="nomProjet" type="java.lang.String">
        <column name="NOMPROJET" />
    </property>
    <set name="scenarios" table="PROJET" inverse="false" lazy="true" order-by="SCENARIO">
        <key>
            <column name="SCENARIO" />
        </key>
        <one-to-many class="com.model.Projet" />
    </set>
   //.....

i'm getting this exception :

exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.model.Projet org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

here is the method :

public ModelAndView saveProject(@RequestParam(value="action", required=false) String action){
 List<Projet> scenarios = projetService.getScenariosByProjet(1);
        model.addObject("scenarios", scenarios);
        List<Projet> sousProjets = projetService.getSsProjetsByProjet(id);
        model.addObject("sousprojets", sousProjets);
        Iterator<Projet> scens = scenarios.iterator();
        while(scens.hasNext()){
            Projet projet2 = (Projet) scens.next();
            System.out.println("projet  scenario : " + projet2.getName);
        }
  return new ModelAndView("/resume","sousprojets", sousProjets);
}
2
Please give full stack trace : somewhere in your code you try to cast to Project an incompatible Object, but currently it is impossible to say where.Serge Ballesta
@SergeBallesta here is all the exception, that bug in the line of the iterator in the controllerOuda
It looks like the exception occurs in method saveProject (Controller.java:847) during a post. You should look (and show us ?) what happens in this method and what the associated form looks likeSerge Ballesta
i post the method and the 'saveProject (Controller.java:847)' : 'Projet projet2 = (Projet) scens.next();'Ouda

2 Answers

2
votes

Use Hibernate template to do query, like this

public Collection<Projet> getScenariosByProjet(int idProjet) {
        return getHibernateTemplate().find("from projet where scenario="+idProjet);
    }
1
votes

Ok, it is clear now : you were using createSQLQuery. The problem is that then query.list() does return a List but the elements of the list are Object arrays (Object[]) with scalar values for each column and not Projet instances. So the exception at runtime.

You simply have to change your query with :

String sql = "from Projet projet where scenario="+idProjet;
Query query = session.createQuery(sql);

and it will work.

BUT : what you are doing is bad : you should never concatenate values in query but allways use prepared queries (simply google or look in SO for SQL injection to understand why ...). It would then turn into :

String sql = "from Projet projet where scenario=?";
Query query = session.createQuery(sql);
query.setInteger(0, id);

which is definitively better.