1 2 Previous Next 25 Replies Latest reply on Jul 25, 2006 8:26 AM by denis-karpov Go to original post
      • 15. Re: Hibernate Validation / Page Scope --> allways new entity
        mrwhite

        Couldnt it be a Scope problem with the property?

        @In(required=false)
         @Out(scope=ScopeType.PAGE,required=false)
         @Valid
         private ClientPerson newClientPerson;
        


        i have no clue.

        • 16. Re: Hibernate Validation / Page Scope --> allways new entity
          bfo81

          Maybe. I only use @In(required=false) and @Out(required=false). If that doesn't help, post your current code ;).

          btw: @Valid is half-deprecated. So try <s:validate(All)> in the JSF instead. That's what Gavin said ;).

          • 17. Re: Hibernate Validation / Page Scope --> allways new entity
            mrwhite

            hey bfo, when i remove the @Valid field i get curiouse class loader exceptions.


            But i think here is a big confusion about the scope of objects (components and their properties).

            I thougt that my property is annotated with scope=ScopeType.PAGE because it "lives" as long as i (or the user) is on the specfic page.

            you can see my code on the first page of this topic.

            • 18. Re: Hibernate Validation / Page Scope --> allways new entity
              bfo81

              The Page Scope is shorter than you think. And it's got nothing to do with the JSF page you're on. This was confusing to me, too ;).

              The code on the first page isn't all I need. And it's been changed. In particular I'd like to see the list page cause it should start the edit conversation.

              • 19. Re: Hibernate Validation / Page Scope --> allways new entity
                mrwhite

                Ok bfo81 lets see:

                My Entity Class:

                package org.jboss.seam.example.client;
                
                import javax.persistence.Entity;
                
                import org.hibernate.validator.Length;
                import org.hibernate.validator.NotNull;
                
                @Entity
                public class ClientPerson extends Client {
                
                 private static final long serialVersionUID = 7829975195914473729L;
                 private String firstname;
                 private String lastname;
                
                 @NotNull
                 @Length(min=2, max=5)
                 public String getFirstname() {
                 return firstname;
                 }
                
                 public void setFirstname(String firstname) {
                 this.firstname = firstname;
                 }
                
                 @NotNull
                 public String getLastname() {
                 return lastname;
                 }
                
                 public void setLastname(String lastname) {
                 this.lastname = lastname;
                 }
                
                
                
                
                
                }
                


                My Edit Action:
                package org.jboss.seam.example.client;
                
                import javax.ejb.Remove;
                import javax.ejb.Stateful;
                import javax.ejb.Stateless;
                import javax.persistence.EntityManager;
                import javax.persistence.PersistenceContext;
                
                import org.hibernate.validator.Valid;
                import org.jboss.seam.ScopeType;
                import org.jboss.seam.annotations.Begin;
                import org.jboss.seam.annotations.Destroy;
                import org.jboss.seam.annotations.End;
                import org.jboss.seam.annotations.Factory;
                import org.jboss.seam.annotations.In;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.Out;
                import org.jboss.seam.annotations.RequestParameter;
                import org.jboss.seam.annotations.Scope;
                
                @Stateful
                @Name("clientPersonAction")
                public class ClientPersonEditAction implements ClientPersonEdit{
                
                 @PersistenceContext
                 private EntityManager em;
                
                 @RequestParameter()
                 private Long clientPersonId;
                
                 @In(required=false)
                 @Out(scope=ScopeType.PAGE,required=false)
                 private ClientPerson newClientPerson;
                
                 @Begin(join=true)
                 @Factory("newClientPerson")
                 public void createNewClientPerson()
                 {
                 if(clientPersonId != null)
                 {
                 ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                 newClientPerson = clientPersonDAO.getClientPersonById(clientPersonId);
                 System.out.println("createNewClientPerson:" + newClientPerson.getId());
                 }
                 else
                 {
                 newClientPerson = new ClientPerson();
                 }
                 }
                
                 @End
                 public String saveClientPerson() {
                 ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                 System.out.println("newClientPerson.getId():" + newClientPerson.getId());
                 clientPersonDAO.merge(newClientPerson);
                 return "home";
                 }
                
                 @Destroy @Remove
                 public void detroy()
                 {
                 }
                
                 @End
                 public String removeClientPerson() {
                 ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                 clientPersonDAO.remove(newClientPerson);
                 return "home";
                 }
                
                }
                



                My DAO Class for the Entity:
                package org.jboss.seam.example.client;
                
                import java.util.List;
                
                import javax.persistence.EntityManager;
                import javax.persistence.Query;
                
                public class ClientPersonDAO {
                
                 private EntityManager entityManager;
                
                 public ClientPersonDAO()
                 {
                
                 }
                
                 public ClientPersonDAO(EntityManager entityManager)
                 {
                 this.entityManager = entityManager;
                 }
                
                 public EntityManager getEntityManager() {
                 return entityManager;
                 }
                
                 public void setEntityManager(EntityManager entityManager) {
                 this.entityManager = entityManager;
                 }
                
                 public void persist(Object obj)
                 {
                 getEntityManager().persist(obj);
                 }
                
                 public void merge(Object obj)
                 {
                 getEntityManager().merge(obj);
                 }
                
                 public List<ClientPerson> getAllClientPerson() {
                 return getEntityManager().createQuery("select c from ClientPerson c").getResultList();
                 }
                
                 public ClientPerson getClientPersonById(long id) {
                 Query q = getEntityManager().createQuery("select c from ClientPerson c where c.id = :id");
                 q.setParameter("id", id);
                 return (ClientPerson) q.getSingleResult();
                 }
                
                 public void remove(ClientPerson clientPerson) {
                 getEntityManager().remove(clientPerson);
                 }
                
                }


                My Facelet page:
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml"
                 xmlns:s="http://jboss.com/products/seam/taglib"
                 xmlns:ui="http://java.sun.com/jsf/facelets"
                 xmlns:f="http://java.sun.com/jsf"
                 xmlns:h="http://java.sun.com/jsf/html">
                <body>
                 <ui:composition template="/WEB-INF/layout/template.xhtml">
                
                 <ui:define name="header">
                 <ui:include src="/WEB-INF/layout/header.xhtml" />
                 </ui:define>
                
                 <ui:define name="body">
                 <h1>Kundenverwaltung</h1>
                 <h:form>
                 <s:validateAll>
                 <div>
                 <h:outputLabel for="firstname">Vorname: </h:outputLabel>
                 </div>
                 <div>
                 <h:inputText id="firstname" value="#{newClientPerson.firstname}" size="70" maxlength="70" required="true"/>
                 <span class="errors"><h:message for="firstname"/></span>
                 </div>
                 <div>
                 <h:outputLabel for="lastname">Nachname: </h:outputLabel>
                 </div>
                 <div>
                 <h:inputText id="lastname" value="#{newClientPerson.lastname}" size="70" maxlength="70" required="true"/>
                 <span class="errors"><h:message for="lastname"/></span>
                 </div>
                 <span>
                 <h:commandButton value="Speichern" action="#{clientPersonAction.saveClientPerson}"/>
                
                
                 </span>
                 </s:validateAll>
                 </h:form>
                
                 <span>
                 <h:form>
                 <h:commandButton value="Entfernen" action="#{clientPersonAction.removeClientPerson}"/>
                 </h:form>
                 </span>
                
                 </ui:define>
                
                 <ui:define name="footer">
                 <ui:include src="/WEB-INF/layout/footer.xhtml" />
                 </ui:define>
                
                 </ui:composition>
                </body>
                </html>
                


                The ClientPerson service that provides the list:
                package org.jboss.seam.example.client;
                
                import java.util.List;
                
                import javax.ejb.Stateful;
                import javax.ejb.Stateless;
                import javax.persistence.EntityManager;
                import javax.persistence.PersistenceContext;
                
                import org.hibernate.validator.Valid;
                import org.jboss.seam.ScopeType;
                import org.jboss.seam.annotations.Factory;
                import org.jboss.seam.annotations.In;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.Out;
                import org.jboss.seam.annotations.RequestParameter;
                
                @Stateless
                @Name("clientPersonService")
                public class ClientPersonServiceImpl implements ClientPersonService {
                
                 @PersistenceContext
                 private EntityManager em;
                
                 public List <ClientPerson> getAllClientPerson()
                 {
                 ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                 return clientPersonDAO.getAllClientPerson();
                 }
                }
                


                The List facelet page/component:
                <div xmlns="http://www.w3.org/1999/xhtml"
                 xmlns:s="http://jboss.com/products/seam/taglib"
                 xmlns:ui="http://java.sun.com/jsf/facelets"
                 xmlns:f="http://java.sun.com/jsf/core"
                 xmlns:h="http://java.sun.com/jsf/html">
                
                 <h1>Kundenverwaltung</h1>
                 <h:dataTable value="#{clientPersonService.allClientPerson}" var="clientPerson" rows="3">
                 <h:column>
                 <h:outputLink value="addClientPerson.seam">
                 #{clientPerson.firstname} #{clientPerson.lastname} - ID: #{clientPerson.id}
                 <f:param name="clientPersonId" value="#{clientPerson.id}"/>
                
                 </h:outputLink>
                 </h:column>
                 </h:dataTable>
                
                </div>




                And thanks bfo81 for your time. ;-)

                • 20. Re: Hibernate Validation / Page Scope --> allways new entity
                  mrwhite

                  Hey you all,
                  i solved my problem you were right bfo81. i had to use the conversation scope for my component and its member ( @Out(scope=ScopeType.CONVERSATION)

                  @Stateful
                  @Name("clientPersonAction")
                  @Scope(ScopeType.CONVERSATION)
                  public class ClientPersonEditAction implements ClientPersonEdit{
                  
                   @PersistenceContext
                   private EntityManager em;
                  
                   @RequestParameter()
                   private Long clientPersonId;
                  
                   @In(required=false)
                   @Out(scope=ScopeType.CONVERSATION,required=false)
                   private ClientPerson newClientPerson;
                  
                   @Begin(join=true)
                   @Factory("newClientPerson")
                   public void createNewClientPerson()
                   {
                   if(clientPersonId != null)
                   {
                   ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                   newClientPerson = clientPersonDAO.getClientPersonById(clientPersonId);
                   System.out.println("createNewClientPerson:" + newClientPerson.getId());
                   }
                   else
                   {
                   newClientPerson = new ClientPerson();
                   }
                   }
                  
                   @End
                   public String saveClientPerson() {
                   ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                   System.out.println("newClientPerson.getId():" + newClientPerson.getId());
                   clientPersonDAO.merge(newClientPerson);
                   return "home";
                   }
                  
                   @Destroy @Remove
                   public void detroy()
                   {
                   }
                  
                   @End
                   public String removeClientPerson() {
                   ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
                   clientPersonDAO.remove(newClientPerson);
                   return "home";
                   }
                  
                  }
                  
                  
                  Thats the problem when you come from other webframeworks. Everybody has their own name for scopes and ways to store information in the session.
                  
                  Its a little bit confusing that every entity, component and their members can have other scopes.
                  
                  thanks
                  
                  
                  


                  • 21. Re: Hibernate Validation / Page Scope --> allways new entity
                    bfo81

                    You still don't need
                    @Out(scope=ScopeType.CONVERSATION, required=false)

                    This is sufficient:
                    @Out(required=false)

                    Why? The ClientPersonEditAction has already the Conversation Scope, and so its properties have that scope, too. You only have to write down a scope for a property explicitly if it differs from its class's scope.

                    And yes: Seam's Scopes are powerful but not always easy to understand. Before developing web apps the easy way you have to learn it the hard way ;).

                    PS: If you would have posted the xhtml containing "Kundenverwaltung" earlier, we could have made a phone call ;). It's really weird how many guys are talking English to each other here (and often having problems with it) though they could have it easier... in Thomas Heute's native language ;D.

                    • 22. Re: Hibernate Validation / Page Scope --> allways new entity
                      mrwhite

                       

                      PS: If you would have posted the xhtml containing "Kundenverwaltung" earlier, we could have made a phone call ;). It's really weird how many guys are talking English to each other here (and often having problems with it) though they could have it easier... in Thomas Heute's native language ;D.



                      You´re right, but due to the situation that this is an english forum we have to write down everything in english to get support.

                      Vielleicht könnte man ja mal eine deutsche Community-Seite gründen?


                      This is sufficient:
                      @Out(required=false)

                      I tested it. When i remove the scope=ScopeType.CONVERSATION the entity id gets lost between the validation and the next submit.




                      • 23. Re: Hibernate Validation / Page Scope --> allways new entity
                        theute

                        Don't get confused by my last name, i am *very* French. Angry, stubborn and such... ;)

                        • 24. Re: Hibernate Validation / Page Scope --> allways new entity
                          denis-karpov

                          Remove @Valid annotation. <s:validateAll> does not require this. Pure hibernate @Valid throws exception on invalid cases, that is not caught by the Seam. That is why you will always get an exception page on invalid data.

                          • 25. Re: Hibernate Validation / Page Scope --> allways new entity
                            denis-karpov

                            Ops. I did not notice that was said already.

                            1 2 Previous Next