[jboss-user] [JBoss Seam] - Re: Hibernate Validation / Page Scope --> allways new entity
mrwhite
do-not-reply at jboss.com
Tue Jul 25 03:32:43 EDT 2006
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. ;-)
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960600#3960600
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960600
More information about the jboss-user
mailing list