Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Erreur "org.hibernate.NonUniqueObjectException"
PostPosted: Thu Aug 25, 2011 6:13 am 
Newbie

Joined: Thu Aug 25, 2011 6:05 am
Posts: 1
Bonjour,

Lors de l'enregistrement dans la base j'ai rencontrer l'erreur suivante:
Quote:
Aug 25, 2011 12:39:35 PM org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider newInstance
INFO: Creating instance of fr.jsf.bean.Arbre
Hibernate: /* SELECT a.applLiblong FROM Application as a */ select applicatio0_.APPL_LIBLONG as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a.id FROM Application a WHERE a.applLiblong=:param */ select applicatio0_.APPL_ID as col_0_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* dynamic native SQL query */ SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=?
Hibernate: /* SELECT a FROM Application as a WHERE a.applLiblong =:param */ select applicatio0_.APPL_ID as APPL1_0_, applicatio0_.APPL_FLAG_ACTIF as APPL2_0_, applicatio0_.APPL_FLAG_VIRTUEL as APPL3_0_, applicatio0_.APPL_GERE_FLUX as APPL4_0_, applicatio0_.APPL_LIBCOURT as APPL5_0_, applicatio0_.APPL_LIBLONG as APPL6_0_, applicatio0_.APPL_PARAM_INSTANCE as APPL7_0_, applicatio0_.APPL_PLANNING as APPL8_0_, applicatio0_.APPL_SERVICE as APPL9_0_ from gedmgpdba.GEDAPPLICATION applicatio0_ where applicatio0_.APPL_LIBLONG=?
Hibernate: /* SELECT p FROM Parametre as p WHERE p.id =:param */ select parametre0_.PARA_MOTCLE as PARA1_1_, parametre0_.PARA_ADMINISTRABLE as PARA2_1_, parametre0_.PARA_APPL_ID as PARA9_1_, parametre0_.PARA_DESCRIPTION as PARA3_1_, parametre0_.PARA_FLAG_FONC as PARA4_1_, parametre0_.PARA_FLAG_TECH as PARA5_1_, parametre0_.PARA_NUM1 as PARA6_1_, parametre0_.PARA_NUM2 as PARA7_1_, parametre0_.PARA_TEXTE1 as PARA8_1_ from gedmgpdba.GEDPARAMETRAGE parametre0_ where parametre0_.PARA_MOTCLE=?
Aug 25, 2011 12:39:35 PM org.apache.myfaces.renderkit.ErrorPageWriter handleThrowable
SEVERE: An exception occurred
javax.faces.el.EvaluationException: javax.el.ELException: /home.xhtml at line 105 and column 118 action="#{parametreBean.save}": org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [fr.jsf.entity.Parametre#PARA_]
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:96)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
at javax.faces.component.UICommand.broadcast(UICommand.java:120)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:978)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:275)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1289)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:716)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at fr.jsf.web.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.el.ELException: /home.xhtml at line 105 and column 118 action="#{parametreBean.save}": org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [fr.jsf.entity.Parametre#PARA_]
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:95)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
... 29 more
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [fr.jsf.entity.Parametre#PARA_]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:638)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:57)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:742)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:722)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
at $Proxy14.update(Unknown Source)
at fr.jsf.bean.ParametreDAOHibernate.alter(ParametreDAOHibernate.java:36)
at fr.jsf.bean.ParametreRN.save(ParametreRN.java:22)
at fr.jsf.bean.ParametreBean.save(ParametreBean.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
... 30 more


Dans ma base j'ai deux tables:
Image

Mon projet est organisé comme suit:
Image

  1. Pour le package fr.jsf.bean :
  • Classe ParametreBean.java
Code:
package fr.jsf.bean;

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIInput;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.apache.myfaces.custom.tree2.TreeNodeBase;
import fr.jsf.entity.Application;
import fr.jsf.entity.Parametre;

@ManagedBean
@SessionScoped
public class ParametreBean {
   
   private Parametre parametre= new Parametre();
   private String selectedItem; 
   private List<SelectItem> selectItems;
   
   public void save(){
      ParametreRN parametreRN= new ParametreRN();
      Application appl=parametreRN.selectApplication(getSelectedItem());
      getParametre();
      parametre.setParaApplId(appl);
      parametre.setParaAdministrable(5);
      parametreRN.save(parametre);
   }
   
   public void alter(){
      ParametreRN parametreRN= new ParametreRN();
      Parametre param=parametreRN.selectParametre(parametre.getId());
      param.setId(parametre.getId());
      param.setParaDescription(parametre.getParaDescription());
      UIInput app = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("param:num1");
      param.setParaNum1(parametre.getParaNum1());
      app = (UIInput) FacesContext.getCurrentInstance().getViewRoot().findComponent("param:num2");
      param.setParaNum2(parametre.getParaNum2());
      param.setParaTexte1(parametre.getParaTexte1());
      parametreRN.alter(param);
   }
   
   public void delete() {
      ParametreRN parametreRN= new ParametreRN();
      Application appl=parametreRN.selectApplication(getSelectedItem());
      parametre.setParaApplId(appl);
      parametreRN.delete(parametre);
   }

   public Parametre getParametre() {
      return parametre;
   }

   public void setParametre(Parametre parametre) {
      this.parametre = parametre;
   }
   
   public void clicLien() throws Exception{   
       
      ParametreRN parametreRN= new ParametreRN();
      FacesContext facesContext = FacesContext.getCurrentInstance();
      ExternalContext extContext = facesContext.getExternalContext();
      TreeNodeBase tnb = (TreeNodeBase) extContext.getRequestMap().get("node");
      Parametre param=parametreRN.selectParametre(tnb.getDescription());
      selectedItem=param.getParaApplId().getApplLiblong();
      setParametre(param);

    }
   
   public String getSelectedItem() { 
         return selectedItem; 
      } 
         
      public void setSelectedItem(String selectedItem) { 
          this.selectedItem = selectedItem; 
      } 
         
      public List<SelectItem> getSelectItems() {
         ParametreRN parametreRN= new ParametreRN();
         List<Application> app=parametreRN.selectAllApplication();
         selectItems = new ArrayList<SelectItem>();
         for(int i=0;i<app.size();i++){
            selectItems.add(new SelectItem(app.get(i).getApplLiblong(), app.get(i).getApplLiblong()));   
         }

          return selectItems; 
      }

      public void setSelectItems(List<SelectItem> selectItems) {
         this.selectItems = selectItems;
      } 
      
}

  • Classe ParametreDAO
Code:
package fr.jsf.bean;

import java.util.List;
import fr.jsf.entity.Application;
import fr.jsf.entity.Parametre;

public interface ParametreDAO {
   
   public void save (Parametre parametre);
   public void delete (Parametre parametre);
   public void alter (Parametre parametre);
   public Application selectApplication(String application);
   public Parametre selectParametre(String parametre);
   public List<Application> selectAllApplication();
   public String selectApplicationId(String application);
   public  List<String> selectParametreId(String parametre);
   public List<Application> selectApplications();
   public String selectParametreIde(String parametre);

}

  • Classe ParametreDAOHibernate
Code:
package fr.jsf.bean;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import fr.jsf.entity.Application;
import fr.jsf.entity.Parametre;

public class ParametreDAOHibernate implements ParametreDAO {
   
   private Session session;

   public Session getSession() {
      return session;
   }

   public void setSession(Session session) {
      this.session = session;
   }

   public void save(Parametre parametre) {
      parametre=(Parametre)session.get(Parametre.class, parametre);
      session.save(parametre);
      session.flush();

   }

   public void delete(Parametre parametre) {
      session.delete(parametre);
      session.flush();

   }

   public void alter(Parametre parametre) {
      session.update(parametre);
      session.flush();

   }
   
   public Application selectApplication(String application){
      
      Query query = session.createQuery("SELECT a FROM Application as a WHERE a.applLiblong =:param").setParameter("param",application);
      return (Application) query.uniqueResult();
   }
   
   public Parametre selectParametre(String parametre){
         
      Query query = session.createQuery("SELECT p FROM Parametre as p WHERE p.id =:param").setParameter("param",parametre);
      return (Parametre) query.uniqueResult();
   }
   
   @SuppressWarnings("unchecked")
   public List<Application> selectAllApplication(){
      
      return session.createCriteria(Application.class).list();
      
   }
   
   public String selectApplicationId(String application){
      
      Query query = session.createQuery("SELECT a.id FROM Application a WHERE a.applLiblong=:param").setParameter("param",application);
      return Integer.toString((Integer) query.uniqueResult());
   }
   
   @SuppressWarnings("unchecked")
   public List<String> selectParametreId(String parametre){
      
      Query query = session.createSQLQuery("SELECT p.PARA_MOTCLE FROM GEDPARAMETRAGE p WHERE p.PARA_APPL_ID=:param").setParameter("param",parametre);
      return (List<String>) query.list();
   }
   
   @SuppressWarnings("unchecked")
   public List<Application> selectApplications(){
      
      Query query = session.createQuery("SELECT a.applLiblong FROM Application as a");
      return  (List<Application>)query.list();
   }
   
   public String selectParametreIde(String parametre){
      
      Query query = session.createQuery("SELECT p.id FROM Parametre p WHERE p.id=:param").setParameter("param",parametre);
      return (String) query.uniqueResult();
   }

}

  • Classe ParametreRN
Code:
package fr.jsf.bean;

import java.util.List;
import fr.jsf.entity.Application;
import fr.jsf.entity.Parametre;
import fr.jsf.hibernate.DAOFactory;

public class ParametreRN {
   
   private ParametreDAO parametreDAO;
   
   public ParametreRN(){
      parametreDAO=DAOFactory.createParametreDAO();
   }

   public void save(Parametre parametre) {
      
      if(parametreDAO.selectParametre(parametre.getId())==null)
         parametreDAO.save(parametre);
      else
         parametreDAO.alter(parametre);
   }

   public void delete(Parametre parametre) {
      parametreDAO.delete(parametre);

   }

   public void alter(Parametre parametre) {
      parametreDAO.alter(parametre);
   }
   
   public Application selectApplication(String application){
      
      return parametreDAO.selectApplication(application);
   }
   
   public Parametre selectParametre(String parametre){
      
      return parametreDAO.selectParametre(parametre);
   }
   
   public List<Application> selectAllApplication(){
      
      return  parametreDAO.selectAllApplication();
   }
   
   public String selectApplicationId(String application){
      
      return parametreDAO.selectApplicationId(application);
   }
   
   public  List<String> selectParametreId(String parametre){
      
      return parametreDAO.selectParametreId(parametre);
   }
   
   public List<Application> selectApplications(){
      return parametreDAO.selectApplications();
   }
   
   public String selectParametreIde(String parametre){
      return parametreDAO.selectParametreIde(parametre);
   }
}

  1. Pour le package fr.jsf.entity:
  • Classe Application
Code:
package fr.jsf.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="GEDAPPLICATION")
public class Application implements Serializable{
   
   private static final long serialVersionUID = 1L;
   @Id
   @Column(name="APPL_ID")
   private Integer id;
   @Column(name="APPL_LIBLONG")
   private String applLiblong;
   @Column(name="APPL_LIBCOURT")
   private String applLibcourt;
   @Column(name="APPL_PLANNING")
   private Integer applPlannig;
   @Column(name="APPL_FLAG_ACTIF")
   private Integer applFlagActif;
   @Column(name="APPL_GERE_FLUX")
   private Integer applGereFlux;
   @Column(name="APPL_PARAM_INSTANCE")
   private Integer applParamInstance;
   @Column(name="APPL_SERVICE")
   private Integer applService;
   @Column(name="APPL_FLAG_VIRTUEL")
   private Integer applFlagVirtuel;
   
   
   @OneToMany(mappedBy = "paraApplId", cascade = { CascadeType.ALL })
   private Set<Parametre> apps =  new HashSet<Parametre>();
   
   public Application(){super();}
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public String getApplLiblong() {
      return applLiblong;
   }
   public void setApplLiblong(String ApplLiblong) {
      this.applLiblong = ApplLiblong;
      
   }
   
   public Set<Parametre> getApps() {
      return apps;
   }
   
   public void setApps(Set<Parametre> apps) {
      this.apps = apps;
   }
   public String getApplLibcourt() {
      return applLibcourt;
   }
   public void setApplLibcourt(String applLibcourt) {
      this.applLibcourt = applLibcourt;
   }
   public Integer getApplPlannig() {
      return applPlannig;
   }
   public void setApplPlannig(Integer applPlannig) {
      this.applPlannig = applPlannig;
   }
   public Integer getApplFlagActif() {
      return applFlagActif;
   }
   public void setApplFlagActif(Integer applFlagActif) {
      this.applFlagActif = applFlagActif;
   }
   public Integer getApplGereFlux() {
      return applGereFlux;
   }
   public void setApplGereFlux(Integer applGereFlux) {
      this.applGereFlux = applGereFlux;
   }
   public Integer getApplParamInstance() {
      return applParamInstance;
   }
   public void setApplParamInstance(Integer applParamInstance) {
      this.applParamInstance = applParamInstance;
   }
   public Integer getApplService() {
      return applService;
   }
   public void setApplService(Integer applService) {
      this.applService = applService;
   }
   public Integer getApplFlagVirtuel() {
      return applFlagVirtuel;
   }
   public void setApplFlagVirtuel(Integer applFlagVirtuel) {
      this.applFlagVirtuel = applFlagVirtuel;
   }
   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result
            + ((applFlagActif == null) ? 0 : applFlagActif.hashCode());
      result = prime * result
            + ((applFlagVirtuel == null) ? 0 : applFlagVirtuel.hashCode());
      result = prime * result
            + ((applGereFlux == null) ? 0 : applGereFlux.hashCode());
      result = prime * result
            + ((applLibcourt == null) ? 0 : applLibcourt.hashCode());
      result = prime * result
            + ((applLiblong == null) ? 0 : applLiblong.hashCode());
      result = prime
            * result
            + ((applParamInstance == null) ? 0 : applParamInstance
                  .hashCode());
      result = prime * result
            + ((applPlannig == null) ? 0 : applPlannig.hashCode());
      result = prime * result
            + ((applService == null) ? 0 : applService.hashCode());
      result = prime * result + ((apps == null) ? 0 : apps.hashCode());
      result = prime * result + ((id == null) ? 0 : id.hashCode());
      return result;
   }
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Application other = (Application) obj;
      if (applFlagActif == null) {
         if (other.applFlagActif != null)
            return false;
      } else if (!applFlagActif.equals(other.applFlagActif))
         return false;
      if (applFlagVirtuel == null) {
         if (other.applFlagVirtuel != null)
            return false;
      } else if (!applFlagVirtuel.equals(other.applFlagVirtuel))
         return false;
      if (applGereFlux == null) {
         if (other.applGereFlux != null)
            return false;
      } else if (!applGereFlux.equals(other.applGereFlux))
         return false;
      if (applLibcourt == null) {
         if (other.applLibcourt != null)
            return false;
      } else if (!applLibcourt.equals(other.applLibcourt))
         return false;
      if (applLiblong == null) {
         if (other.applLiblong != null)
            return false;
      } else if (!applLiblong.equals(other.applLiblong))
         return false;
      if (applParamInstance == null) {
         if (other.applParamInstance != null)
            return false;
      } else if (!applParamInstance.equals(other.applParamInstance))
         return false;
      if (applPlannig == null) {
         if (other.applPlannig != null)
            return false;
      } else if (!applPlannig.equals(other.applPlannig))
         return false;
      if (applService == null) {
         if (other.applService != null)
            return false;
      } else if (!applService.equals(other.applService))
         return false;
      if (apps == null) {
         if (other.apps != null)
            return false;
      } else if (!apps.equals(other.apps))
         return false;
      if (id == null) {
         if (other.id != null)
            return false;
      } else if (!id.equals(other.id))
         return false;
      return true;
   }
}

  • Classe Parametre
Code:
package fr.jsf.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="GEDPARAMETRAGE")
public class Parametre implements Serializable{

   private static final long serialVersionUID = 1L;
   @Id
   @Column(name="PARA_MOTCLE")
   private String id;
   @Column(name="PARA_TEXTE1")
   private String paraTexte1;
   @Column(name="PARA_DESCRIPTION")
   private String paraDescription;
   @Column(name="PARA_NUM1")
   private Integer  paraNum1;
   @Column(name="PARA_NUM2")
   private Integer  paraNum2;
   @Column(name="PARA_ADMINISTRABLE")
   private Integer  paraAdministrable;
   @Column(name="PARA_FLAG_FONC")
   private Integer  paraFlagFonc;
   @Column(name="PARA_FLAG_TECH")
   private Integer  paraFlagTech;
   
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name = "PARA_APPL_ID", nullable = false)
   private Application paraApplId;
   

   
   public Parametre(){super();}
   
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }

   public Application getParaApplId() {
      return paraApplId;
   }

   public void setParaApplId(Application paraApplId) {
      this.paraApplId = paraApplId;
   }

   public String getParaTexte1() {
      return paraTexte1;
   }

   public void setParaTexte1(String paraTexte1) {
      this.paraTexte1 = paraTexte1;
   }

   public String getParaDescription() {
      return paraDescription;
   }

   public void setParaDescription(String paraDescription) {
      this.paraDescription = paraDescription;
   }

   public Integer getParaNum1() {
      return paraNum1;
   }

   public void setParaNum1(Integer paraNum1) {
      this.paraNum1 = paraNum1;
   }

   public Integer getParaNum2() {
      return paraNum2;
   }

   public void setParaNum2(Integer paraNum2) {
      this.paraNum2 = paraNum2;
   }

   public Integer getParaFlagFonc() {
      return paraFlagFonc;
   }

   public void setParaFlagFonc(Integer paraFlagFonc) {
      this.paraFlagFonc = paraFlagFonc;
   }

   public Integer getParaFlagTech() {
      return paraFlagTech;
   }

   public void setParaFlagTech(Integer paraFlagTech) {
      this.paraFlagTech = paraFlagTech;
   }

   public Integer getParaAdministrable() {
      return paraAdministrable;
   }

   public void setParaAdministrable(Integer paraAdministrable) {
      this.paraAdministrable = paraAdministrable;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((id == null) ? 0 : id.hashCode());
      result = prime
            * result
            + ((paraAdministrable == null) ? 0 : paraAdministrable
                  .hashCode());
      result = prime * result
            + ((paraApplId == null) ? 0 : paraApplId.hashCode());
      result = prime * result
            + ((paraDescription == null) ? 0 : paraDescription.hashCode());
      result = prime * result
            + ((paraFlagFonc == null) ? 0 : paraFlagFonc.hashCode());
      result = prime * result
            + ((paraFlagTech == null) ? 0 : paraFlagTech.hashCode());
      result = prime * result
            + ((paraNum1 == null) ? 0 : paraNum1.hashCode());
      result = prime * result
            + ((paraNum2 == null) ? 0 : paraNum2.hashCode());
      result = prime * result
            + ((paraTexte1 == null) ? 0 : paraTexte1.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      Parametre other = (Parametre) obj;
      if (id == null) {
         if (other.id != null)
            return false;
      } else if (!id.equals(other.id))
         return false;
      if (paraAdministrable == null) {
         if (other.paraAdministrable != null)
            return false;
      } else if (!paraAdministrable.equals(other.paraAdministrable))
         return false;
      if (paraApplId == null) {
         if (other.paraApplId != null)
            return false;
      } else if (!paraApplId.equals(other.paraApplId))
         return false;
      if (paraDescription == null) {
         if (other.paraDescription != null)
            return false;
      } else if (!paraDescription.equals(other.paraDescription))
         return false;
      if (paraFlagFonc == null) {
         if (other.paraFlagFonc != null)
            return false;
      } else if (!paraFlagFonc.equals(other.paraFlagFonc))
         return false;
      if (paraFlagTech == null) {
         if (other.paraFlagTech != null)
            return false;
      } else if (!paraFlagTech.equals(other.paraFlagTech))
         return false;
      if (paraNum1 == null) {
         if (other.paraNum1 != null)
            return false;
      } else if (!paraNum1.equals(other.paraNum1))
         return false;
      if (paraNum2 == null) {
         if (other.paraNum2 != null)
            return false;
      } else if (!paraNum2.equals(other.paraNum2))
         return false;
      if (paraTexte1 == null) {
         if (other.paraTexte1 != null)
            return false;
      } else if (!paraTexte1.equals(other.paraTexte1))
         return false;
      return true;
   }

}

  1. Pour le package fr.jsf.hibernate:
  • Classe DAOFactory
Code:
package fr.jsf.hibernate;
import fr.jsf.bean.ParametreDAO;
import fr.jsf.bean.ParametreDAOHibernate;

public class DAOFactory {
   
   public static ParametreDAO createParametreDAO(){
      
      ParametreDAOHibernate parametreDAO = new ParametreDAOHibernate();
      parametreDAO.setSession(HibernateUtil.getSf().getCurrentSession());
      return parametreDAO;
   }

}

  • Classe HibernateUtil
Code:
package fr.jsf.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
   
   private static final SessionFactory sF = buildSessionFactory();

   public static SessionFactory buildSessionFactory(){
      
      try{
         Configuration cfg = new Configuration();
         cfg.configure("hibernate.cfg.xml");
         return cfg.buildSessionFactory();
      }catch(Throwable ex){
         System.out.println("Initial SessionFactory creation failed."+ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public static SessionFactory getSf(){
      
      return sF;
   }

}

  1. Pour le package fr.jsf.web.filter:
  • Classe HibernateSessionRequestFilter
Code:
package fr.jsf.web.filter;

import java.io.IOException;
import javax.servlet.*;
import fr.jsf.hibernate.HibernateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;

public class HibernateSessionRequestFilter implements Filter {
   
    private static Log log = LogFactory.getLog(HibernateSessionRequestFilter.class);

    private SessionFactory sf;

    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {

        try {
            log.debug("Starting a database transaction");
            sf.getCurrentSession().beginTransaction();

            // Call the next filter (continue request processing)
            chain.doFilter(request, response);

            // Commit and cleanup
            log.debug("Committing the database transaction");
            sf.getCurrentSession().getTransaction().commit();

        } catch (StaleObjectStateException staleEx) {
            log.error("This interceptor does not implement optimistic concurrency control!");
            log.error("Your application will not work until you add compensation actions!");
            // Rollback, close everything, possibly compensate for any permanent changes
            // during the conversation, and finally restart business conversation. Maybe
            // give the user of the application a chance to merge some of his work with
            // fresh data... what you do here depends on your applications design.
            throw staleEx;
        } catch (Throwable ex) {
            // Rollback only
            ex.printStackTrace();
            try {
                if (sf.getCurrentSession().getTransaction().isActive()) {
                    log.debug("Trying to rollback database transaction after exception");
                    sf.getCurrentSession().getTransaction().rollback();
                }
            } catch (Throwable rbEx) {
                log.error("Could not rollback transaction after exception!", rbEx);
            }

            // Let others handle it... maybe another interceptor for exceptions?
            throw new ServletException(ex);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("Initializing filter...");
        log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
        sf = HibernateUtil.getSf();
    }

    public void destroy() {}

}


Merci d'avance pour vos réponses.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.