Announcement Announcement Module
Collapse
No announcement yet.
relation between tables Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • relation between tables

    Hello,
    I wanted to respect the constraints of integrities between my tables, so i added the relation many-to-one. I believed that if I did that, if I remove something of the first table it will be reflected on the second table. but if i do that i have an exception
    this my hbm files :
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
        
    <hibernate-mapping>
    <!-- 
        Created by the Middlegen Hibernate plugin 2.2
    
        http://boss.bekk.no/boss/middlegen/
        http://www.hibernate.org/
    -->
    
    <class 
        name="com.pia.agp.mapping.Intervenant" 
        table="intervenant"
        lazy="false"
    >
    
        <id
            name="intervenantId"
            type="java.lang.Integer"
            column="INTERVENANT_ID"
        >
        
            <generator class="increment" />
        </id>
    
        <property
            name="intervenantNom"
            type="java.lang.String"
            column="INTERVENANT_NOM"
            length="30"
        />
        <property
            name="intervenantPrenom"
            type="java.lang.String"
            column="INTERVENANT_PRENOM"
            length="30"
        />
        <property
            name="intervenantLogin"
            type="java.lang.String"
            column="INTERVENANT_LOGIN"
            length="30"
        />
        <property
            name="intervenantMdp"
            type="java.lang.String"
            column="INTERVENANT_MDP"
            length="30"
        />
        <property
            name="intervenantMdp2"
            type="java.lang.String"
            column="INTERVENANT_MDP2"
            length="30"
        />
        <property
            name="intervenantEmail"
            type="java.lang.String"
            column="INTERVENANT_EMAIL"
            length="30"
        />
        <property
            name="intervenantTel"
            type="java.lang.String"
            column="INTERVENANT_TEL"
            length="15"
        />
        <property
            name="intervenantFonction"
            type="java.lang.String"
            column="INTERVENANT_FONCTION"
            length="20"
        />
        <property
            name="intervenantDatedebutcontrat"
            type="java.sql.Date"
            column="INTERVENANT_DATEDEBUTCONTRAT"
            not-null="true"
            length="10"
        />
        <property
            name="intervenantDatefincontrat"
            type="java.sql.Date"
            column="INTERVENANT_DATEFINCONTRAT"
            not-null="true"
            length="10"
        />
    
    
    <!-- 
        <property
            name="coutjournalierexperianId"
            type="java.lang.String"
            column="COUTJOURNALIEREXPERIAN_ID"
            not-null="true"
            length="32"
        />
    
        
    
        <property
            name="droitId"
            type="java.lang.Long"
            column="DROIT_ID"
            not-null="true"
        />
    	<many-to-one
                name="coutjournalierexperianId"    
                class="com.pia.agp.mapping.Coutjournalierexperian"
                cascade="none"
                outer-join="auto"
                update="true"
                insert="true"
                column="COUTJOURNALIEREXPERIAN_ID"
                not-null="true"
        />
    	
    	<many-to-one
                name="droitId"    
                class="com.pia.agp.mapping.Droit"
                cascade="none"
                outer-join="auto"
                update="true"
                insert="true"
                column="DROIT_ID"
                not-null="true"
        />
        
    -->	
    	<many-to-one 
    		name="coutjournalierexperian"
    		class="com.pia.agp.mapping.Coutjournalierexperian" 
    		column="COUTJOURNALIEREXPERIAN_ID"
    		
    	/>
    	
    	<many-to-one 
    		name="droit"
    		class="com.pia.agp.mapping.Droit" 
    		column="DROIT_ID"
    		
    	/>
    	
    
        <!-- Associations -->
      
    
    </class>
    </hibernate-mapping>

  • #2
    and the second file :
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
        
    <hibernate-mapping>
    <!-- 
        Created by the Middlegen Hibernate plugin 2.2
    
        http://boss.bekk.no/boss/middlegen/
        http://www.hibernate.org/
    -->
    
    <class 
        name="com.pia.agp.mapping.Droit" 
        table="droit"
        lazy="false"
    >
    
        <id
            name="droitId"
            type="java.lang.Long"
            column="DROIT_ID"
        >
        
            <generator class="increment" />
        </id>
    
        <property
            name="droitDroit"
            type="java.lang.String"
            column="DROIT_DROIT"
            length="30"
        />
    
    </class>
    </hibernate-mapping>
    how can i do that ?
    thanks for your help

    Comment


    • #3
      What is the exception you get ? Try setting cascade attribute of many-to-one association... I think the default value for it is 'none'.

      Comment


      • #4
        Hi,
        the exception :
        Code:
        Etat HTTP 500 -
        
        type Rapport d'exception
        
        message
        
        description Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.
        
        exception
        
        org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [com.pia.agp.mapping.Coutjournalierexperian#DEV2]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pia.agp.mapping.Coutjournalierexperian#DEV2]
        	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:406)
        	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:348)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        
        cause mère
        
        org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [com.pia.agp.mapping.Coutjournalierexperian#DEV2]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pia.agp.mapping.Coutjournalierexperian#DEV2]
        	org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:634)
        	org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:413)
        	org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:371)
        	org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:844)
        	org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:836)
        	com.pia.agp.dao.DaoIntervenantImpl.getAll(DaoIntervenantImpl.java:25)
        	com.pia.agp.service.ServiceIntervenantImpl.getAll(ServiceIntervenantImpl.java:30)
        	com.pia.agp.web.ListIntervenant.handleRequest(ListIntervenant.java:32)
        	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
        	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
        	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
        	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
        	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:348)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        
        cause mère
        
        org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.pia.agp.mapping.Coutjournalierexperian#DEV2]
        	org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
        	org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:145)
        	org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:179)
        	org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
        	org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
        	org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
        	org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
        	org.hibernate.type.EntityType.resolve(EntityType.java:379)
        	org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
        	org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
        	org.hibernate.loader.Loader.doQuery(Loader.java:729)
        	org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
        	org.hibernate.loader.Loader.doList(Loader.java:2220)
        	org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
        	org.hibernate.loader.Loader.list(Loader.java:2099)
        	org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
        	org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
        	org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
        	org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
        	org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
        	org.springframework.orm.hibernate3.HibernateTemplate$31.doInHibernate(HibernateTemplate.java:853)
        	org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
        	org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:844)
        	org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:836)
        	com.pia.agp.dao.DaoIntervenantImpl.getAll(DaoIntervenantImpl.java:25)
        	com.pia.agp.service.ServiceIntervenantImpl.getAll(ServiceIntervenantImpl.java:30)
        	com.pia.agp.web.ListIntervenant.handleRequest(ListIntervenant.java:32)
        	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
        	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
        	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
        	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
        	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:348)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        
        note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/5.5.23.
        Apache Tomcat/5.5.23
        for cascade i must add this :
        Code:
        cascade="cascade_style"
        thanks
        ?

        Comment


        • #5
          What exactly want to achieve ? When deleting a Intervenant record, you want Droit and Coutjournalierexperian deleted also ?

          Comment


          • #6
            Hi,
            the opposite, When I delete Coutjournalierexperian or Droit record, i want Intervenant deleted also.

            thanks

            Comment


            • #7
              Hello,
              as I did not find a solution, I tried to make my self the suppression.
              Code:
              	public void deleteOne(String id) {
              		List r = getHibernateTemplate().find(
              				"from Coutjournalierexperian where COUTJOURNALIEREXPERIAN_ID = "+"\'"+id+"\'");
              		getHibernateTemplate().delete( (Coutjournalierexperian) r.get(0));
              		// on supprime aussi tous les enregistrements qui contiennent cette clé
              		// Dans la table Intervenant
              		List req = getHibernateTemplate().find(
              				"from Intervenant where COUTJOURNALIEREXPERIAN_ID = "+"\'"+id+"\'");
              		getHibernateTemplate().delete( (Coutjournalierexperian) req.get(0));
              	}
              I had then the same exception . Sohibernate did it too. I believe that the problem comes from my classes java
              this Intervenant.java
              Code:
              package com.pia.agp.mapping;
              
              import java.io.Serializable;
              import java.util.Date;
              import org.apache.commons.lang.builder.ToStringBuilder;
              
              
              /** @author Hibernate CodeGenerator */
              public class Intervenant implements Serializable {
              
                  /** identifier field */
                  private Integer intervenantId;
              
                  /** nullable persistent field */
                  private String intervenantNom;
              
                  /** nullable persistent field */
                  private String intervenantPrenom;
              
                  /** nullable persistent field */
                  private String intervenantLogin;
              
                  /** nullable persistent field */
                  private String intervenantMdp;
                  
                  private String intervenantMdp2;
              
                  /** nullable persistent field */
                  private String intervenantEmail;
              
                  /** nullable persistent field */
                  private String intervenantTel;
              
                  /** nullable persistent field */
                  private String intervenantFonction;
              
                  /** persistent field */
                  private Date intervenantDatedebutcontrat;
              
                  /** persistent field */
                  private Date intervenantDatefincontrat;
              
                  /** persistent field */
                  private String coutjournalierexperianId;
              
                  /** persistent field */
                  private long droitId;
                  
                  
                  private Droit droit;
                  private Coutjournalierexperian coutjournalierexperian;
                  
                  
                  /** full constructor */
                  public Intervenant(Integer intervenantId, String intervenantNom, String intervenantPrenom, String intervenantLogin, String intervenantMdp,String intervenantMdp2, String intervenantEmail, String intervenantTel, String intervenantFonction, Date intervenantDatedebutcontrat, Date intervenantDatefincontrat, String coutjournalierexperianId, long droitId,Droit droit,Coutjournalierexperian coutjournalierexperian) {
                      this.intervenantId = intervenantId;
                      this.intervenantNom = intervenantNom;
                      this.intervenantPrenom = intervenantPrenom;
                      this.intervenantLogin = intervenantLogin;
                      this.intervenantMdp = intervenantMdp;
                      this.intervenantMdp2 = intervenantMdp2;
                      this.intervenantEmail = intervenantEmail;
                      this.intervenantTel = intervenantTel;
                      this.intervenantFonction = intervenantFonction;
                      this.intervenantDatedebutcontrat = intervenantDatedebutcontrat;
                      this.intervenantDatefincontrat = intervenantDatefincontrat;
                      this.coutjournalierexperianId = coutjournalierexperianId;
                      this.droitId = droitId;
                      this.droit = droit;
                      this.coutjournalierexperian = coutjournalierexperian;
                  }
              
                  /** default constructor */
                  public Intervenant() {
                  }
              
                  /** minimal constructor */
                  public Intervenant(Integer intervenantId, Date intervenantDatedebutcontrat, Date intervenantDatefincontrat, String coutjournalierexperianId, long droitId,Droit droit,Coutjournalierexperian coutjournalierexperian) {
                      this.intervenantId = intervenantId;
                      this.intervenantDatedebutcontrat = intervenantDatedebutcontrat;
                      this.intervenantDatefincontrat = intervenantDatefincontrat;
                      this.coutjournalierexperianId = coutjournalierexperianId;
                      this.droitId = droitId;
                      this.droit = droit;
                      this.coutjournalierexperian = coutjournalierexperian;
                  }
              
                  public Intervenant(Integer intervenantId) {
                      this.intervenantId = intervenantId;
                  }
                  
                  public Integer getIntervenantId() {
                      return this.intervenantId;
                  }
              
                  public void setIntervenantId(Integer intervenantId) {
                      this.intervenantId = intervenantId;
                  }
              
                  public String getIntervenantNom() {
                      return this.intervenantNom;
                  }
              
                  public void setIntervenantNom(String intervenantNom) {
                      this.intervenantNom = intervenantNom;
                  }
              
                  public String getIntervenantPrenom() {
                      return this.intervenantPrenom;
                  }
              
                  public void setIntervenantPrenom(String intervenantPrenom) {
                      this.intervenantPrenom = intervenantPrenom;
                  }
              
                  public String getIntervenantLogin() {
                      return this.intervenantLogin;
                  }
              
                  public void setIntervenantLogin(String intervenantLogin) {
                      this.intervenantLogin = intervenantLogin;
                  }
              
                  public String getIntervenantMdp() {
                      return this.intervenantMdp;
                  }
              
                  public void setIntervenantMdp(String intervenantMdp) {
                      this.intervenantMdp = intervenantMdp;
                  }
              
                  public String getIntervenantMdp2() {
                      return this.intervenantMdp2;
                  }
              
                  public void setIntervenantMdp2(String intervenantMdp2) {
                      this.intervenantMdp2 = intervenantMdp2;
                  }
                  
                  public String getIntervenantEmail() {
                      return this.intervenantEmail;
                  }
              
                  public void setIntervenantEmail(String intervenantEmail) {
                      this.intervenantEmail = intervenantEmail;
                  }
              
                  public String getIntervenantTel() {
                      return this.intervenantTel;
                  }
              
                  public void setIntervenantTel(String intervenantTel) {
                      this.intervenantTel = intervenantTel;
                  }
              
                  public String getIntervenantFonction() {
                      return this.intervenantFonction;
                  }
              
                  public void setIntervenantFonction(String intervenantFonction) {
                      this.intervenantFonction = intervenantFonction;
                  }
              
                  public Date getIntervenantDatedebutcontrat() {
                      return this.intervenantDatedebutcontrat;
                  }
              
                  public void setIntervenantDatedebutcontrat(Date intervenantDatedebutcontrat) {
                      this.intervenantDatedebutcontrat = intervenantDatedebutcontrat;
                  }
              
                  public Date getIntervenantDatefincontrat() {
                      return this.intervenantDatefincontrat;
                  }
              
                  public void setIntervenantDatefincontrat(Date intervenantDatefincontrat) {
                      this.intervenantDatefincontrat = intervenantDatefincontrat;
                  }
              
                  public String getCoutjournalierexperianId() {
                      return this.coutjournalierexperianId;
                  }
              
                  public void setCoutjournalierexperianId(String coutjournalierexperianId) {
                      this.coutjournalierexperianId = coutjournalierexperianId;
                  }
              
                  public long getDroitId() {
                      return this.droitId;
                  }
              
                  public void setDroitId(long droitId) {
                      this.droitId = droitId;
                  }
              
                  public Droit getDroit() {
                      return this.droit;
                    }
              
                   public void setDroit(Droit droit) {
                      this.droit=droit;
                    }
                   
                   public Coutjournalierexperian getCoutjournalierexperian() {
                       return this.coutjournalierexperian;
                     }
              
                    public void setCoutjournalierexperian(Coutjournalierexperian coutjournalierexperian) {
                       this.coutjournalierexperian = coutjournalierexperian;
                     }
                    
                    
                  public String toString() {
                      return new ToStringBuilder(this)
                          .append("intervenantId", getIntervenantId())
                          .toString();
                  }
              
              }
              Before adding the relation many-to-one, I had my two foreign keys like String (COUTJOURNALIER_ID) and long (DROIT_ID).
              but someone told me that i should put like type the object of the keys because Hibernate can make all the connection alone, So I added Droit DROIT_ID and COUT…
              maybe the problem come from there .

              thank you very much

              Comment


              • #8
                This should be quite straight forward, if you delete an entity and you have the cascade setup correctly it will delete the children. I'd have a look at the reference manual.
                Last edited by karldmoore; Aug 27th, 2007, 04:03 PM.

                Comment


                • #9
                  There's some information on cascades here.
                  http://www.hibernate.org/hib_docs/v3...tion-manytoone
                  Last edited by karldmoore; Aug 27th, 2007, 04:03 PM.

                  Comment

                  Working...
                  X