Search code examples
javahibernatehqlcriteria

Execute a HQL request


I tried to execute the following request in HQL:

session1.createQuery("select p from Projet p inner join p.domaine d where p.site = :site")
                            .setString("site", selected)
                            .list(); 

But in the result i'm getting the id of the table domaine, not the value of the column that i want to display which is a String. PS/I have a many to one relation between domaine and pojet. Have i forget to add sometnig

Projet.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 25 avr. 2015 06:50:10 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="bean.Projet" table="PROJET">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="libelle" type="java.lang.String">
            <column name="LIBELLE" />
        </property>
        <property name="site" type="java.lang.String">
            <column name="SITE" />
        </property>
        <property name="finalite" type="java.lang.String">
            <column name="FINALITE" />
        </property>
        <property name="date_actuelle" type="java.lang.String">
            <column name="DATE_ACTUELLE" />
        </property>
        <property name="date_debut" type="java.lang.String">
            <column name="DATE_DEBUT" />
        </property>
        <property name="date_previsionnelle" type="java.lang.String">
            <column name="DATE_PREVISIONNELLE" />
        </property>
        <many-to-one name="domaine" class="bean.Domaine" fetch="join">
            <column name="IDDOMAINE" />
        </many-to-one>
        <set name="assignedActeurs" table="PROJET_ACTEUR" inverse="false"
            lazy="true" fetch="select" cascade="all">
            <key>
                <column name="ID" />
            </key>
            <many-to-many class="bean.Acteur">
                <column name="IDACTEUR" not-null="true" />
            </many-to-many>
        </set>
        <set name="assignedBénéficiaires" table="PROJET_BENEFICIAIRE"
            inverse="false" lazy="true" fetch="select" cascade="all">
            <key>
                <column name="ID" />
            </key>
            <many-to-many class="bean.Bénéficiaire">
                <column name="IDBENEFICIAIRE" not-null="true" />
            </many-to-many>
        </set>

        <many-to-one name="chefprojet" class="bean.Chefprojet"
            fetch="join">
            <column name="ID_CHEF" />
        </many-to-one>

    </class>
</hibernate-mapping>

Domaine.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 25 avr. 2015 06:50:10 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="bean.Domaine" table="DOMAINE">
        <id name="idDomaine" type="int">
            <column name="IDDOMAINE" />
            <generator class="identity" />
        </id>
        <property name="type_domaine" type="java.lang.String">
            <column name="TYPE_DOMAINE" />
        </property>
        <set name="assignedProjets" table="PROJET" inverse="false" lazy="true">
            <key>
                <column name="IDDOMAINE" />
            </key>
            <one-to-many class="bean.Projet" />
        </set>
        <set name="assignedChantiers" table="CHANTIER" fetch="select"
            cascade="all">
            <key>
                <column name="IDDOMAINE" not-null="true" />
            </key>
            <one-to-many class="bean.Chantier" />
        </set>

    </class>
</hibernate-mapping>

liste.jsp

<html>

<head>


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="acceuil.css" type="text/css" />
<link rel="stylesheet" href="ajoutprojet.css" type="text/css" />

<script type="text/javascript"
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" /></script>

</head>

<%
    List projects = (List) request.getAttribute("listProject");
    if (projects == null)
        projects = new ArrayList();
%>

<body>
    <div class="page">
        <div class="header">
            <a href="index.html" id="logo"><img src="images/Captureocp.PNG"
                alt="" /></a>
            <ul>
                <li><a href="acceuil.jsp">Home</a></li>
                <li><a href="about.html">About</a></li>
                <li><a href="blog.html">Blog</a></li>
                <li class="selected"><a href="services.html">Services</a></li>
            </ul>
        </div>
    </div>
    </br>
    </br>

    <form method="post" action="liste">
        <p>
            <select name="sites">
                <option value="">-- Choisir --</option>
                <option value="Khouribga">Khouribga</option>
                <option value="Casablanca">Casablanca</option>
                <option value="Casablanca">Jorf Lasfar</option>
                <option value="Casablanca">Benguerir</option>
                <option value="Casablanca">Boucraa</option>
                <option value="Casablanca">Youssoufia</option>
                <option value="Casablanca">Laayoune</option>
                <option value="Casablanca">Safi</option>

            </select><br />

        </p>
        <div class="centre">

            <input type="submit" value="Chercher" name="Chercher" />
        </div>


        <table border="1" cellpadding="3pt" align="center">
            <tr>
                <td>Liste des projets :</td>
            </tr>


            <c:forEach var="projet" items="${listProject}">
                <tr>
                    <td><c:out value="${projet.libelle}" /></td>
                    <td><c:out value="${projet.site}" /></td>
                    <td><c:out value="${projet.finalite}" /></td>
                    <td><c:out value="${projet.date_actuelle}" /></td>
                    <td><c:out value="${projet.date_debut}" /></td>
                    <td><c:out value="${projet.date_previsionnelle}" /></td>
                    <td><c:out value="${projet.domaine.idDomaine}" /></td>
                    <td><c:out value="${projet.chefprojet.id_chef}" /></td>

                </tr>
            </c:forEach>

        </table>

    </form>
</body>
</html>

la Servlet:

public class listeControllerServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {


        HttpSession session = request.getSession(true);


        try {
            Session session1=HibernateUtil.getSessionFactory().getCurrentSession();
            String selected=request.getParameter("sites");
            session1.beginTransaction();
            List<Projet> list = 
                    session1.createQuery("select p.id, p.site,p.libelle,p.finalite,p.date_actuelle,p.date_debut,p.date_previsionnelle, d.type_domaine from Projet p inner join p.domaine d where p.site = :site")
                            .setString("site", selected)
                            .list();
            request.setAttribute("listProject", list);
            request.getRequestDispatcher("liste.jsp").forward(request, response);
            session1.close(); 

        } catch (NumberFormatException e) {

            e.printStackTrace();
        }   
    }
}

Solution

  • From what I understand you want to select all the columns from the Project table and only one column from the Domaine table, so you need to explicitly pass all those columns in the select query like this:

    session1.createQuery("select p.id, p.libelle, p.site, p.finalite, p.date_actuelle, d.type_domaine from Projet p inner join p.domaine d where p.site = :site")
                            .setString("site", selected)
                            .list(); 
    

    EDIT:

    When you use this query you will not get a list of Projects but instead a list of Objects because you are merging columns from Project and Domaine so you should need to change your code like this:

    Servlet:

    List<Object> list = 
                    session1.createQuery("select p.id, p.site,p.libelle,p.finalite,p.date_actuelle,p.date_debut,p.date_previsionnelle, d.type_domaine from Projet p inner join p.domaine d where p.site = :site")
                            .setString("site", selected)
                            .list();
    

    JSP:

    And in your jsp you get the results using object[index] where index is the attribute index in your select part of the query, here's what you need:

    <c:forEach var="projet" items="${listProject}">
         <tr>
             <td><c:out value="${projet[2]}" /></td>
             <td><c:out value="${projet[1]}" /></td>
             <td><c:out value="${projet[3]}" /></td>
             <td><c:out value="${projet[4]}" /></td>
             <td><c:out value="${projet[5]}" /></td>
             <td><c:out value="${projet[6]}" /></td>
             <!-- you should include idDomaine and id_chef in your query to get them here -->
             <td><c:out value="${projet.domaine.idDomaine}" /></td>
             <td><c:out value="${projet.chefprojet.id_chef}" /></td>
    
        </tr>
    </c:forEach>