CRUD update problem
mekonom.meko16.hotmail.com Aug 9, 2009 9:14 PMHi ..I have an little problem .In my Example I want to edit my user detail .but every time when push Update button it creates new data like new user..but I want to update existing user. :( I think problem on updatePerson() action ..
here is my codes
This is person class
import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.jboss.seam.annotations.Name; @Entity @Table(name = "PERSON") public class Person { private Long id; public String firstname; public String lastname; public String phonenumber; public String addres; private Set<PersonLesson> personLessons = new HashSet<PersonLesson>(0); @Id @GeneratedValue @Column(name = "PERSON_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "FIRST_NAME") public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } @Column(name = "LAST_NAME") public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } @Column(name = "PHONE_NUMBER") public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } @Column(name = "ADDRES") public String getAddres() { return addres; } public void setAddres(String addres) { this.addres = addres; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person") public Set<PersonLesson> getPersonLessons() { return personLessons; } public void setPersonLessons(Set<PersonLesson> personLessons) { this.personLessons = personLessons; } }
This is PersonUtil class
import java.util.ArrayList; import java.util.List; import javax.persistence.EntityManager; import org.jboss.seam.Component; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; @Name("personutil") @Scope(ScopeType.CONVERSATION) public class PersonUtil { private Person activePerson ; private List<Person> persons = new ArrayList<Person>(); private Lesson newLesson ; public PersonUtil() { activePerson = new Person(); initPersonList(); } public void activePerson(Person kisi){ this.activePerson = kisi ; } public Lesson getNewLesson() { return newLesson; } public void setNewLesson(Lesson newLesson) { this.newLesson = newLesson; } public Person getActivePerson() { return activePerson; } public void setActivePerson(Person activePerson) { this.activePerson = activePerson; } public void initPersonList() { EntityManager em = (EntityManager) Component.getInstance("entityManager"); List<Person> result = em.createQuery("from Person") .getResultList(); setPersons(result); } public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } public void initPerson(Person person) { this.activePerson = person; } public void savePerson() { EntityManager em = (EntityManager) Component.getInstance("entityManager"); em.persist(activePerson); em.flush(); } public void removePerson(Person person) { EntityManager em = (EntityManager) Component.getInstance("entityManager"); em.remove(person); em.flush(); } public void updatePerson(Person person) { EntityManager em = (EntityManager) Component.getInstance("entityManager"); em.persist(person); em.flush(); } }
This is existing user XHTML
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"> <h:form> <rich:dataTable onRowMouseOver="this.style.backgroundColor='#F8F8F8'" onRowMouseOut="this.style.backgroundColor='#{a4jSkin.tableBackgroundColor}'" width="400" var="kisi" value="#{personutil.persons}"> <f:facet name="header"> <rich:columnGroup> <rich:column rowspan="1"> <h:outputText value="KISILER" /> </rich:column> <rich:column colspan="4"> <h:outputText value="DUZEN" /> </rich:column> </rich:columnGroup> </f:facet> <rich:column rowspan="1"> <h:outputText value="#{kisi.firstname}#{kisi.lastname}" /> </rich:column> <rich:column> <a4j:commandButton action="#{personutil.removePerson(kisi)}" value="Sil" /> </rich:column> <rich:column> <a4j:commandButton action="#{personutil.activePerson(kisi)}" value="Edit" /> </rich:column> <rich:column> <a4j:commandButton action="#{personlessonutil.activePerson(kisi)}" value="Dersleri" /> </rich:column> </rich:dataTable> </h:form> </ui:composition>
And the last for update page..After pressing edit button it goes this page
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"> <h:form> <f:facet name="header"> <rich:columnGroup> <rich:column colspan="4"> <rich:spacer /> </rich:column> <rich:column colspan="1"> <h:outputText value="DUZEN" /> </rich:column> <rich:column breakBefore="true"> <h:outputText value="Ad" /> </rich:column> <rich:column> <h:outputText value="Soyad" /> </rich:column> <rich:column> <h:outputText value="Telefon" /> </rich:column> <rich:column> <h:outputText value="Adress" /> </rich:column> </rich:columnGroup> </f:facet> <rich:column> <h:inputText value="#{personutil.activePerson.firstname}" /></rich:column> <rich:column> <h:inputText value="#{personutil.activePerson.lastname}"></h:inputText> </rich:column> <rich:column> <h:inputText value="#{personutil.activePerson.phonenumber}"></h:inputText> </rich:column> <rich:column> <h:inputText value="#{personutil.activePerson.addres}"></h:inputText> </rich:column> <rich:column> <a4j:commandButton action="#{personutil.updatePerson()}" value="Update" /> </rich:column> </h:form> </ui:composition>