[jboss-user] [EJB/JBoss] - Re: EntityManager merge and the seam-registration example.

hchafi do-not-reply at jboss.com
Fri Jul 21 01:54:50 EDT 2006


  | 
  | import static org.jboss.seam.ScopeType.SESSION;
  | 
  | import java.io.Serializable;
  | ...
  | import org.jboss.seam.annotations.Scope;
  | 
  | @Entity
  | @Name("user")
  | @Scope(SESSION)
  | @Table(name="users",
  | 	uniqueConstraints={@UniqueConstraint(columnNames={"username"})})
  | public class User implements Serializable {
  | 
  | 	
  | 	private static final long serialVersionUID = 9093363133205674398L;
  | 	
  | 	private Long id;
  | 	private String username = null;
  | 	private String password = null;
  | 	private Boolean seller = null;
  | 	private SellerInfo sellerInfo = null;
  | 	private ContactInfo contactInfo = null;
  | 	private String firstName;
  | 	private String lastName;
  | 	
  | 	public String getFirstName() {
  | 		return firstName;
  | 	}
  | 	public void setFirstName(String firstName) {
  | 		this.firstName = firstName;
  | 	}
  | 	public String getLastName() {
  | 		return lastName;
  | 	}
  | 	public void setLastName(String lastName) {
  | 		this.lastName = lastName;
  | 	}
  | 	
  | 
  | 	public ContactInfo getContactInfo() {
  | 		return contactInfo;
  | 	}
  | 	public void setContactInfo(ContactInfo contactInfo) {
  | 		this.contactInfo = contactInfo;
  | 	}
  | 	
  | 
  | 	public User() {
  | 		super();
  | 	}
  | 	
  | 	
  | 	@NotNull @Length(min=5, max=15)
  | 	public String getPassword() {
  | 		return password;
  | 	}
  | 	public void setPassword(String password) {
  | 		this.password = password;
  | 	}
  | 	
  | 	@NotNull @Length(min=5, max=15)
  | 	public String getUsername() {
  | 		return username;
  | 	}
  | 	public void setUsername(String username) {
  | 		this.username = username;
  | 	}
  | 	public SellerInfo getSellerInfo() {
  | 		return sellerInfo;
  | 	}
  | 	public void setSellerInfo(SellerInfo sellerInfo) {
  | 		this.sellerInfo = sellerInfo;
  | 	}
  | 	
  | 	@Id @GeneratedValue(strategy=GenerationType.AUTO)
  | 	public Long getId() {
  | 		return id;
  | 	}
  | 	public void setId(Long id) {
  | 		this.id = id;
  | 	}
  | 	public Boolean getSeller() {
  | 		return seller;
  | 	}
  | 	public void setSeller(Boolean seller) {
  | 		this.seller = seller;
  | 	}
  | 	
  | 
  | }
  | 

My impression was that @Scope did the binding.

The remaining code is almost identical to initial example code, but for completness sake.


  | <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  | <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  | <%@ taglib uri="http://jboss.com/products/seam/taglib" prefix="s" %>
  | <html>
  |  <head>
  |   <title>Register New User</title>
  |  </head>
  |  <body>
  |   <f:view>
  |    <h:form>
  |      <table border="0">
  |        <s:validateAll>
  |          <tr>
  |            <td>Username</td>
  |            <td><h:inputText value="#{user.username}" required="true"/></td>
  |          </tr>
  |          <tr>
  |            <td>First Name</td>
  |            <td><h:inputText value="#{user.firstName}" required="true"/></td>
  |          </tr>
  |          <tr>
  |            <td>Last Name</td>
  |            <td><h:inputText value="#{user.lastName}" required="true"/></td>
  |          </tr>
  |          <tr>
  |            <td>Password</td>
  |            <td><h:inputSecret value="#{user.password}" required="true"/></td>
  |          </tr>
  |        </s:validateAll>
  |      </table>
  |      <h:messages/>
  |      <h:commandButton type="submit" value="Register" action="#{register.register}"/>
  |    </h:form>
  |   </f:view>
  |  </body>
  | </html>
  | 

Finally the RegisterAction, all I changed is the em.persist to em.merge


  | import java.util.List;
  | 
  | import javax.ejb.Stateless;
  | ...
  | import org.jboss.seam.log.Log;
  | 
  | import com.genietown.model.User;
  | 
  | @Stateless
  | @Name("register")
  | public class RegisterAction implements Register
  | {
  | 
  |    @In @Valid
  |    private User user;
  |    
  |    @PersistenceContext
  |    private EntityManager em;
  |    
  |    @Logger
  |    private Log log;
  |    
  |    public String register()
  |    {
  |       List existing = em.createQuery("select username from User where username=:username")
  |          .setParameter("username", user.getUsername())
  |          .getResultList();
  |       if (existing.size()==0)
  |       {
  |          em.merge(user);
  |          log.info("Registered new user #{user.username}");
  |          return "/registered.jsp";
  |       }
  |       else
  |       {
  |          FacesMessages.instance().add("User #{user.username} already exists");
  |          return null;
  |       }
  |    }
  | 
  | }
  | 

Thanks for your help. To restate the question, why wouldn't merge simply update the user info instead of creating a new user? Obviously the later is what I want, but I am not sure why this works the way it is setup currently.

-hc

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

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



More information about the jboss-user mailing list