执行 HQL 请求

Execute a HQL request

我尝试在 HQL 中执行以下请求:

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

但结果我得到的是 table 域的 ID,而不是我想显示的列的值,它是一个字符串。 PS/I domaine 和 pojet 之间存在多对一关系。 我忘记添加一些东西了吗

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 小服务程序:

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

查询应如下所示:

select p.id, p.libelle, ...(all the columns you need), d.type_domaine from Projet p inner join p.domaine d where p.site = :site

其中 desiderColumn 是您想要 select

的单个列的名称

据我了解,您想要 select Project table 中的所有列,而 Domaine table 中只有一列,所以您需要像这样在 select 查询中显式传递所有这些列:

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(); 

编辑:

当您使用此查询时,您不会获得 Project 的列表,而是 Object 的列表,因为您正在合并来自 Project 和 [=14= 的列] 所以你需要像这样更改你的代码:

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:

并且在您的 jsp 中,您使用 object[index] 获得结果,其中 index 是查询的 select 部分中的属性索引,这是您需要的:

<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>