0 Replies Latest reply on Jun 10, 2008 1:00 PM by henri kameni

    Error by persisting entity beans with n:m relationship

    henri kameni Newbie

      I get the following error by persisting an entity bean:


      11:56:08,316 FATAL [application] javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: org.domain.ConfigBaukasten.entity.ParamInstance




      my bean entities are:


      - Configuration.java


      @Entity
      @Name("configuration")
      public class Configuration implements Serializable {
              
              //seam-gen attributes (you should probably edit these)
              private Long id;
              private Integer version;
              private String name;
              
              private List<ParamInstance> params = new ArrayList<ParamInstance>();
              
                      
              @Id @GeneratedValue
              public Long getId() {
                   return id;
              }
      
              public void setId(Long id) {
                   this.id = id;
              }
              
              @Version
              public Integer getVersion() {
                   return version;
              }
      
              private void setVersion(Integer version) {
                   this.version = version;
              }       
              
              @Length(max=20)
              public String getName() {
                   return name;
              }
      
              public void setName(String name) {
                   this.name = name;
              } 
              
              @OneToMany(mappedBy="configuration", cascade=CascadeType.ALL)
              public List<ParamInstance> getParams() {
                      return params;
              }
              
              public void setParams(List<ParamInstance> params) {
                      this.params = params;
              }
      }




      - ParamInstance.java



      @Entity
      public class ParamInstance implements Serializable {
              
              //seam-gen attributes (you should probably edit these)
              private Long id;
              private Integer version;
              private String name;
              
          private Parameter parameter;
              private int value;
              private Date validFrom;
              private Date validUntil;
              private Configuration configuration;
              
                      
              @Id @GeneratedValue
              public Long getId() {
                   return id;
              }
      
              public void setId(Long id) {
                   this.id = id;
              }
              
              @Version
              public Integer getVersion() {
                   return version;
              }
      
              private void setVersion(Integer version) {
                   this.version = version;
              }       
              
              @Length(max=20)
              public String getName() {
                   return name;
              }
      
              public void setName(String name) {
                   this.name = name;
              }
      
              @OneToOne(fetch=FetchType.LAZY, optional=false)    
              @JoinColumn(name = "parameter_id")
              public Parameter getParameter() {
                      return parameter;
              }
      
              public void setParameter(Parameter parameter) {
                      this.parameter = parameter;
              }
      
              public int getValue() {
                      return value;
              }
      
              public void setValue(int value) {
                      this.value = value;
              }
      
              public Date getValidFrom() {
                      return validFrom;
              }
      
              public void setValidFrom(Date validFrom) {
                      this.validFrom = validFrom;
              }
      
              public Date getValidUntil() {
                      return validUntil;
              }
      
              public void setValidUntil(Date validUntil) {
                      this.validUntil = validUntil;
              }
      
              @ManyToOne(cascade=CascadeType.ALL)
              @JoinColumn(name="configuration")
              public Configuration getConfiguration() {
                      return configuration;
              }
      
              public void setConfiguration(Configuration configuration) {
                      this.configuration = configuration;
              }       
      }



      and session bean is:



      @Stateful
      @Name("ParamAction")
      public class ParamActionBean implements ParamAction {
              
              @Logger private Log log;
              
          @PersistenceContext(type = EXTENDED)
          private EntityManager em;
          
          //@In
         // private Configuration configuration;
          
          @Out (required=false) 
          private List<SelectItem> parameters = null;
          
          @Out (required=false)
          private List<SelectItem> paramInsts = null;
          
          
          
          @Factory("parameters")
          public void findParameters() {
              parameters = new ArrayList<SelectItem>();
              
              Map<String, Parameter> allParamsMapped = new HashMap<String,Parameter>();
              
              Parameter param;
      
              for(Iterator<Parameter> it = ((List<Parameter>)em.createQuery("select p from Parameter p").getResultList()).iterator(); it.hasNext();){
              
                      SelectItem item = new SelectItem();
                      param = (Parameter)it.next();
                  item.setValue( param);
                  item.setLabel((param.getId()).toString());
                 parameters.add( item );
                 allParamsMapped.put(param.getName(), param);
              }
              Contexts.getConversationContext().set("allParametersMapped", allParamsMapped);
          }
          
          @Factory("paramInsts")
          public void findParamInstances(){
              paramInsts = new ArrayList<SelectItem>();
              ParamInstance paramInst;
              Map<String, ParamInstance> allParamInstsMapped = new HashMap<String,ParamInstance>();
              
              for(Iterator<ParamInstance> it = ((List<ParamInstance>)em.createQuery("select pi from ParamInstance pi").getResultList()).iterator(); it.hasNext();){
                      
                      SelectItem item = new SelectItem();
                      
                      paramInst = (ParamInstance)it.next();
                      
                      item.setValue(paramInst);
                      item.setLabel(paramInst.getName());
                      
                      paramInsts.add(item);
                      allParamInstsMapped.put(paramInst.getName(), paramInst);
              }
              
              Contexts.getConversationContext().set("allParamInstsMapped", allParamInstsMapped);
          }
          
         /* public String save(){
              em.merge(configuration);
              return "";
          }*/
         
          
              public List<SelectItem> getParameters() {
              
                      return parameters;
              }
      
              public void setParameters(List<SelectItem> parameters) {
                      this.parameters = parameters;
              }
              
              @Remove
              @Destroy
              public void destroy() {}
      }



      The xhtml is:


      <ui:define name="body">
         
          <h:messages globalOnly="true" styleClass="message"/>
         
          <h:form id="configurationForm">

              <rich:panel>
                  <f:facet name="header">configuration</f:facet>
         
                  <s:decorate id="nameDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Name</ui:define>
                      <h:inputText id="name" required="true"
                                   value="#{configurationHome.instance.name}"/>
                  </s:decorate>
                  <s:decorate id="ParamInstsDecoration" template="layout/edit.xhtml">
                      <ui:define name="label">Parameter Instances</ui:define>
                      <h:selectManyMenu value="#{configurationHome.instance.params}" converter="ParamInstConverter">
                           <f:selectItems value="#{paramInsts}" />
                      </h:selectManyMenu>

                  </s:decorate>
                 
                  <div style="clear:both"/>
                 
              </rich:panel>

              <div class="actionButtons">
                  <h:commandButton id="save"
                                value="Save"
                               action="#{configurationHome.persist}"
                             rendered="#{!configurationHome.managed}"/>                  
                  <h:commandButton id="update"
                                value="Save"
                               action="#{configurationHome.update}"
                             rendered="#{configurationHome.managed}"/>                 
                  <h:commandButton id="delete"
                                value="Delete"
                               action="#{configurationHome.remove}"
                             rendered="#{configurationHome.managed}"/>
                  <s:button propagation="end"
                                     id="done"
                                  value="Done"
                                   view="/configurationList.xhtml"/>
              </div>
             
          </h:form>
         
      </ui:define>


      any idea?