[jboss-user] [JBoss Seam] - conversation???

constantin.cash do-not-reply at jboss.com
Mon Nov 5 06:25:38 EST 2007


Instance of User will be saved twice,

service:

@Stateful
  | @Name("inseract")
  | public class InserAct implements InserActI{
  |     @Logger private Log log;
  |     
  |     @In private FacesMessages facesMessages;
  |     
  |     @In private EntityManager entityManager;
  |     
  |   //  @In private Context sessionContext;
  |     
  |     @In(required=false) Artikel artikel;
  |     
  |     @In private Identity identity;
  |     
  |     
  |     private Kategorie kat;
  |     
  |     private Kategorie ukat;
  |      
  |     private List<Kategorie> ukats;
  |  
  |     private byte[] data;
  |     private String contentType;
  |     private String fileName;
  |     private String title;
  |     private int size;
  |     
  |     
  |     public String weiter(){
  |         return "/fupload";
  |     }
  |     
  |     @Remove @Destroy
  |     public void remove(){
  |         
  |     }
  |     
  |     //@End
  |     public String upload() {
  |         Img img = new Img();
  |         img.setEdata(data);
  |         img.setEtype(contentType);
  |         img.setEsize(size);
  |         
  |         User usr = null;
  |         try{
  |             usr = (User) entityManager.createQuery("select u from User as u where u.email = :email")
  |             .setParameter("email",identity.getUsername())
  |             .getSingleResult();
  |            
  |             artikel.getImgs().add(img);
  |             img.setArt(artikel);
  |             artikel.setUsr(usr);
  |             artikel.setKat(kat);
  |            
  |             //entityManager.merge(artikel);
  |             
  |            
  |             usr.getArts().add(artikel);
  |              artikel = null;
  |              entityManager.merge(usr);
  |              usr =null;
  |             return "/upokay";
  |         }catch(Exception ex){
  |             log.error(ex);
  |             return null;
  |         }
  |         
  |         
  |     }

view:

  |  <h:form>
  |                 <s:validateAll>
  |                     <table width="800px" cellspacing="3" cellpadding="3">
  |                         <tr style="margin:0px;padding:0px 0px">
  |                             <td style="margin:0px;padding:0px 0px">Titel:</td>
  |                             <td valign="top" class="star">
  |                                 <h:inputText id="tit"  styleClass="txt" size="30" maxlength="100"  value="#{artikel.titel}" required="true"/> *
  |                                 <h:message for="tit" styleClass="errors"/>
  |                             </td>
  |                         </tr>
  |                         <tr>
  |                             
  |                             <td> Kategorie:</td>
  |                             <td valign="top" class="star">
  |                                 <a4j:region id="rgs" renderRegionOnly="false">
  |                                     <h:selectOneMenu value="#{inseract.kat}" >
  |                                         <s:selectItems value="#{sservice.katLIST}" required="true" var="kat" label="#{messages[kat.rkey]}" noSelectionLabel="Bitte wählen..."/>
  |                                         <a4j:support event="onchange" reRender="ukats" >
  |                                             <s:conversationPropagation type="join" />
  |                                         </a4j:support>
  |                                     </h:selectOneMenu>
  |                                     <h:message for="her" styleClass="errors"/>
  |                                 </a4j:region> *
  |                             </td>
  |                         </tr>
  |                       <!--
  |                         <tr>
  |                             
  |                             <td> Unterkategorie:</td>
  |                             <td valign="top" class="star">
  |                                 <s:span id="ukats">
  |                                     <h:selectOneMenu id="rgs1" required="true" value="#{inseract.ukat}" >
  |                                         <s:selectItems value="#{inseract.ukats}" var="kat" label="#{messages[kat.rkey]}" noSelectionLabel="Bitte wählen..."/>
  |                                     </h:selectOneMenu>
  |                                     <h:message for="rgs1" styleClass="errors"/>
  |                                 </s:span> *
  |                             </td>
  |                         </tr>
  |                         
  |                         -->
  |                         
  |                         
  |                         
  |                         <tr>
  |                             
  |                             <td> Hersteller:</td>
  |                             <td valign="top" class="star">
  |                                 <h:inputText id="her"  styleClass="txt" size="30" maxlength="100"  value="#{artikel.hersteller}" required="true"/> *
  |                                 <h:message for="her" styleClass="errors"/>
  |                             </td>
  |                         </tr>
  |                         
  |                         
  |                         <tr>
  |                             <td valign="top" >Model: </td>
  |                             
  |                             <td valign="top" class="star">
  |                                 
  |                                 <h:inputText id="art"  styleClass="txt" size="30" maxlength="100"  value="#{artikel.model}" required="true"/>  *
  |                                 <h:message for="art" styleClass="errors"/>
  |                             </td>
  |                         </tr>
  |                         <tr>
  | 
  | ...
  | 
  |    <tr>
  |                             <td colspan="2" align="left">
  |                             <p> <h:commandButton styleClass="button" value="weiter" action="#{inseract.weiter()}" reRender="frm,msg" /></p></td>
  |                         </tr>
  | 


model:


  | @Entity
  | @Name("usr")
  | @Table(name="users")
  | public class User implements java.io.Serializable {
  |     
  |     @Id @GeneratedValue(strategy = GenerationType.AUTO)
  |     private Long id;
  |   
  |     @NotNull private int anrede;
  |     
  |     @NotNull private String firma;
  |     private String str;
  |     private String plz;
  |     private String ort;
  |     
  |     @Email @NotNull private String email;
  |    
  |     @NotNull @Length(min=6, max=30) private String passwort;
  |     
  |     @OneToMany(mappedBy="usr",cascade = CascadeType.ALL)
  |     private List<Artikel> arts;
  |     
  | 





  | @Entity
  | @Name("artikel")
  | @Table(name="artikel")
  | public class Artikel {
  |     @Id @GeneratedValue(strategy = GenerationType.AUTO)
  |     private Long id;
  |     
  |     private String hersteller;
  |     
  |     private String typ;
  |     
  |     private String model;
  |     
  |     private double preis;
  |     
  |     private String titel;
  |     
  |     private double gewicht;
  |     
  |     @NotNull @ManyToOne
  |     private User usr;
  |     
  |     @NotNull @ManyToOne
  |     private Kategorie kat;
  |     
  |     @Length(max=1000)
  |     private String beschreibung;
  |     
  |     private int bs;
  |     
  |     private String standort;
  |     
  |     private boolean activ;
  |      
  |     @OneToMany(mappedBy="art",cascade = CascadeType.ALL)
  |     private List<Img> imgs;



  | 
  | @Entity
  | @Name("image")
  | @Table(name="images")
  | public class Img {
  |     
  |     @Id @GeneratedValue(strategy = GenerationType.AUTO)
  |     private Long id;
  |  
  |     @Lob  @Basic(fetch=FetchType.LAZY)
  |     private byte[] edata;
  |     
  |     @NotNull @ManyToOne
  |     private Artikel art;
  |     
  |     private String etype;
  |     
  |     private int esize;
  | 
  |    @Override

if change the lines in the service to
 

  | public String upload() {
  |         Img img = new Img();
  |         img.setEdata(data);
  |         img.setEtype(contentType);
  |         img.setEsize(size);
  |         
  |         User usr = null;
  |         try{
  |             usr = (User) entityManager.createQuery("select u from User as u where u.email = :email")
  |             .setParameter("email",identity.getUsername())
  |             .getSingleResult();
  |            
  |             artikel.getImgs().add(img);
  |             img.setArt(artikel);
  |             artikel.setUsr(usr);
  |             artikel.setKat(kat);
  |            
  |             entityManager.merge(artikel);
  |             
  |            
  |             //usr.getArts().add(artikel);
  |              artikel = null;
  |              //entityManager.merge(usr);
  |              //usr =null;
  |             return "/upokay";
  |         }catch(Exception ex){
  |             log.error(ex);
  |             return null;
  |         }
  |         
  |         
  |     }

it does work correctly, if i make away artikel=null, the instance of artiekel and img will saved twice, 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4101710#4101710

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4101710




More information about the jboss-user mailing list