tags:

views:

118

answers:

2
Exception during request processing:
Caused by javax.servlet.ServletException with message: "#{registration.register}: javax.ejb.EJBTransactionRolledbackException"
javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:44)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:141)
org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
java.lang.Thread.run(Unknown Source)

My Code is As Follows:

Registeration.java

package org.jboss.seam.example.registration;

import javax.ejb.Local;

@Local
public interface Registeration {
    public void register();
    public void invalid();
    public String getVerify();
    public void setVerify(String verify);
    public boolean isRegistered();
    public void destroy();
}

RegistrationAction.java

package org.jboss.seam.example.registration;

import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.type.TrueFalseType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;

@Stateful
@Name("registration")
public class RegistrationAction implements Registeration {
    @In(create=true ,required=false)
    private UserRegisteration user;
    @PersistenceContext
    EntityManager em;
    @In(create=true,required=false)
    private FacesMessages facesmessages;
    private String verify;
    private boolean registered;

    public void register() {
        if(user.getPassword().equals(verify)) {
            List existing=em.createQuery("SELECT u.username from UserRegisteration u WHERE u.username=#{user.username}").getResultList();
            if (existing.size()==0) {
                em.persist(user);
                facesmessages.add("Successfully registered as #{user.username}");
                registered = true;
            } else {
                facesmessages.addToControl("username", "Username #{user.username} already exists");
            }
        } else {
            facesmessages.addToControl("verify", "Re-enter your password");
            verify=null;
        }
    }

    public void invalid() {
        facesmessages.add("Please try again");
    }

    public boolean isRegistered() {
        return registered;
    }

    public String getVerify() {
        return verify;
    }

    public void setVerify(String verify) {
        this.verify = verify;
    }

    @Remove
    public void destroy() {}
}

UserRegisteration.java

package org.jboss.seam.example.registration;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.Session;
import org.hibernate.annotations.Generated;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import static org.jboss.seam.ScopeType.SESSION;

@Entity
@Name("user")
@Scope(SESSION)
@Table(name="Registration")
public class UserRegisteration implements Serializable{
    private int id;
    private String username;
    private String firstname;
    private String password;
    private String city;
    private String state;
    private String country;
    private String zip;
    private String phoneno;

    public UserRegisteration(int id,String username,String firstname,String password,String city, String state,String country,String Zip,String phoneno) {
        this.id=id;
        this.username=username;
        this.firstname=firstname;
        this.password=password;
        this.city=city;
        this.state=state;
        this.country=country;
        this.zip=zip;
        this.phoneno=phoneno;
    }

    public UserRegisteration(){}

    @Id 
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @NotNull
    @Length(max=50)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @NotNull
    @Length(max=50)
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    @NotNull
    @Length(min=5,max=10)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @NotNull
    @Length(max=50)
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @NotNull
    @Length(max=50)
    public String getState() {
        return state;
    }

    public void setState(String state) {
    this.state = state;
    }

    @NotNull
    @Length(max=50)
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @NotNull
    @Length(min=4,max=5)
    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    @NotNull
    @Length(max=10)
    public String getPhoneno() {
        return phoneno;
    }

    public void setPhoneno(String phoneno) {
        this.phoneno = phoneno;
    }

    @Override
    public String toString() {
        return "UserRegisteration (" + username + ")";
    }
}

register.xhtml

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<ui:composition 
    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"
    xmlns:rich="http://richfaces.org/rich"
    template="layout/template.xhtml">
    <ui:define name="body">
        <h:form >
            <rich:panel>
                <f:facet name="header">Registration</f:facet>
                <div class="dialog">
                    <s:validateAll>
                        <h:panelGrid columns="2">
                            UserName: <h:inputText value="#{user.username}"  required="true"/>
                            FirstName: <h:inputText value="#{user.firstname}" required="true"/>
                            Password:<h:inputSecret value="#{user.password}"  required="true"/>
                            VerifyPassword:<h:inputSecret value="#{registration.verify}" required="true"/>
                            City: <h:inputText value="#{user.city}" required="true"/>
                            State: <h:inputText value="#{user.state}" required="true"/>
                            Country: <h:inputText value="#{user.country}" required="true"/>
                            Zip: <h:inputText value="#{user.zip}" required="true"/>
                            PhoneNo: <h:inputText value="#{user.phoneno}" required="true"/>
                        </h:panelGrid>
                    </s:validateAll>
                    <h:commandButton value="Register" action="#{registration.register}"/>
                </div>
            </rich:panel>
        </h:form>
    </ui:define> 
</ui:composition>
A: 

Hi,

  • You cannot hardcode EL expression in a Query:

    List existing=em.createQuery("SELECT u.username from UserRegisteration u WHERE u.username=#{user.username}").getResultList();

  • You should define a property of the user which has to be unique (name + first name, or email so on). And check for equality of this property not comparing the user from DB with the user from the UI.

Cristian Boariu
@Cristian Boariu, Hi Cristian. You can use inline Expression Language. But to get this feature, you have to use @In instead of @PersistenceContext.
Arthur Ronald F D Garcia
A: 

Take a look at this Tutorial: http://download.oracle.com/javaee/5/tutorial/doc/bnbqw.html

ckuetbach