-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Criteria sur une relation Many to Many
PostPosted: Wed Jun 22, 2016 10:11 am 
Newbie

Joined: Wed Jun 22, 2016 8:02 am
Posts: 3
Bonjour,
J'ai deux tables (PERSON et PROJECT) qui sont reliées par une table de liaison (PROJECT_PERSON)

PERSON
PK person_id
Person_name

PROJECT
PK project_id
Project_name

Table de relation PROJECT_PERSON :
PROJECT_PERSON
PK(project_id, person_id, role_id)

J'ai également une autre table ROLE qui liste tous les roles possibles
ROLE
PK role_id
role_name


Voici mon fichier de config hibernate :
Code:
<class name="com.capgemini.deliveryreport.entities.Project" table="PROJECT">
      <id name="projectId" column="PROJECT_ID" type="int" unsaved-value="null">
         <generator class="sequence">
            <param name="sequence">PROJECT_PROJECT_ID_seq</param>
         </generator>
      </id>       
       <set name="projectsHistory" table="PROJECT_HISTORY" inverse="true" lazy="true" >
          <key column="PROJECT_ID" />
          <one-to-many class="com.capgemini.deliveryreport.entities.ProjectHistory"/>
       </set>
       <set name="persons" table="PROJECT_PERSON" inverse="false" lazy="true" cascade="all">
          <key>
             <column name="PROJECT_ID" not-null="true"/>
          </key>
          <many-to-many class="com.capgemini.deliveryreport.entities.Person">
            <column name="PERSON_ID" not-null="true"/>
         </many-to-many>       
       </set>
       
       <property name="domain" column="DOMAIN" type="string" />
       <property name="customer_name" column="CUSTOMER_NAME" type="string" />
      <property name="name" column="NAME" type="string" />
      <property name="gfs" column="GFS" type="int" />
      <property name="status" column="STATUS" type="short" />
      <property name="trend" column="TREND" type="short" />
      <property name="planning" column="PLANNING" type="short" />
      <property name="finance" column="FINANCE" type="short" />
      <property name="customer" column="CUSTOMER" type="short" />
      <property name="human" column="HUMAN" type="short" />
      <property name="international" column="INTERNATIONAL" type="short" />
      <property name="comment" column="COMMENT" type="string" />      
      
      <property name="actions" column="ACTIONS" type="string" />
      <property name="co_div" column="CO_DIV" type="string" />
      <property name="suivi" column="SUIVI" type="string" />
      
      <property name="person_id" column="PERSON_ID" type="int" />
      <property name="last_edit" column="LAST_EDIT" type="calendar" />
      <property name="week_num" column="WEEK_NUM" type="calendar" />
   </class>

<class name="com.capgemini.deliveryreport.entities.Person" table="PERSON">
      <id name="personId" column="PERSON_ID" type="int" unsaved-value="null">
         <generator class="sequence">
            <param name="sequence">PERSON_PERSON_ID_seq</param>
         </generator>      
      </id>   
      <many-to-one name="profil" class="com.capgemini.deliveryreport.entities.Profil" >
         <column name="PROFIL_ID" not-null="true"/>
      </many-to-one>      
      <many-to-one name="role" class="com.capgemini.deliveryreport.entities.Role" >
         <column name="ROLE_ID" not-null="true"/>
      </many-to-one>         
      
      <set name="projects" table="PROJECT_PERSON" inverse="false" lazy="true" cascade="all">
          <key>
             <column name="PERSON_ID" not-null="true"/>
          </key>
          <many-to-many class="com.capgemini.deliveryreport.entities.Project">
            <column name="PROJECT_ID" not-null="true"/>
         </many-to-many>       
       </set>
             
      <property name="personName" column="PERSON_NAME" type="string" />
      <property name="firstName" column="FIRST_NAME" type="string" />
      <property name="lastName" column="LAST_NAME" type="string" />
      <property name="password" column="PASSWORD" type="string" />      
   </class>
<class name="com.capgemini.deliveryreport.entities.Role" table="ROLE">
      <id name="roleId" column="ROLE_ID" type="int" unsaved-value="null">
         <generator class="sequence">
            <param name="sequence">ROLE_ROLE_ID_seq</param>
         </generator>      
      </id>
      <property name="roleName" column="ROLE_NAME" type="string" />   
      <set name="persons" table="PERSON" inverse="true" lazy="true" >
          <key column="PERSON_ID" />
          <one-to-many class="com.capgemini.deliveryreport.entities.Person"/>
       </set>
   </class>


Pour obtenir la liste de tous les projets associés à une personne, j'utilise l'API criteria

Code:
   public List <Project> getAllProject(int person_id, int role_id) {   
      Session session = sessionFactory.getCurrentSession();
      
      Criteria criteria = session.createCriteria(Project.class);
      criteria.addOrder(Order.desc("last_edit"));   

      //association many-to-many Set<Person> persons
      Criteria criteriaP = criteria.createCriteria("persons");
      criteriaP.add(Restrictions.eq("personId", person_id));   
      
      List<Project> listAllProject = criteria.list();
      return listAllProject;
   }

Jusque là, c'est bon pour moi.
Seulement voilà je voudrais rajouter une condition sur la colonne <role_id> de la table PROJECT_PERSON :
role_id = yy par exemple

Sachant que je n'ai pas défini de classe pour la table <PROJECT_PERSON> dans mon projet, comment accéder a la colonne <role_id> avec les criteria ?

Merci


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.