Hibernate Validation / Page Scope --> allways new entity
mrwhite Jul 21, 2006 10:46 AMHi everybody,
i got a strange data manipulation problem.
I have a simple form i use for editing and inserting:
<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>
<div>
<h:commandButton value="Speichern" action="#{clientPersonAction.saveClientPerson}"/>
</div>
</s:validateAll>
</h:form>
And i have the following Action:
@Stateless
@Name("clientPersonAction")
public class ClientPersonEditAction implements ClientPersonEdit{
@PersistenceContext
private EntityManager em;
@RequestParameter()
private Long clientPersonId;
@In(required=false)
@Out(scope=ScopeType.PAGE,required=false)
@Valid
private ClientPerson newClientPerson;
@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();
}
}
public String saveClientPerson() {
ClientPersonDAO clientPersonDAO = new ClientPersonDAO(em);
System.out.println("newClientPerson.getId():" + newClientPerson.getId());
clientPersonDAO.merge(newClientPerson);
return "home";
}
}
The Entity
package org.jboss.seam.example.client;
import java.io.Serializable;
import javax.persistence.Entity;
import org.hibernate.validator.NotNull;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Scope;
@Scope(ScopeType.EVENT)
@Entity
public class ClientPerson extends Client implements Serializable{
private static final long serialVersionUID = -7846741936105611777L;
private String firstname;
private String lastname;
@NotNull
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;
}
}
When I fill out the form and save a new object its correctly saved in the database. When some id is passed to the action i can edit the values from the database object and save everything back.
BUT: When i open a persisted object from the database and remove the field content from a field that has for example the hibernate validator annotation @NotNull i get the normal message "value required". Then i type in correct data and press the save button a new entity is persisted with all the other fields from the "old" entity but under a different id.
Summary: Adding and editing works. Saving a persisted object after a hibernate validation error creates a new one in the database.
Any suggestions on that?