Seam Database Problem
pavan.pavankkotturi.kumar.gmail.com Mar 18, 2010 9:00 AMWhen ever I am tried to insert the data into the data base, the data was not inserted and also it was not showing any errors.
Please look at below code,
AccountAction.java
import static javax.persistence.PersistenceContextType.EXTENDED;
import static org.jboss.seam.ScopeType.EVENT;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
@Stateful
@Scope(EVENT)
@Name("accountInformation")
public class AccountAction implements AccountInformation
{
@In
private Account account;
@PersistenceContext
private EntityManager em;
@In
private FacesMessages facesMessages;
private String verify;
private boolean registered;
public void register()
{
if ( account.getName().equals(verify) )
{
List existing = em.createQuery("select name from Account where name=#{account.name} ")
.getResultList();
if (existing.size()==0)
{
em.persist(account);
facesMessages.add("Successfully registered as #{account.name}");
registered = true;
}
else
{
facesMessages.addToControl("name", "Name #{account.name} already exists");
}
}
}
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() {}
}
AccountInformation.java
import javax.ejb.Local;
@Local
public interface AccountInformation
{
public void register();
public void invalid();
public String getVerify();
public void setVerify(String verify);
public boolean isRegistered();
public void destroy();
}
Account.java
package org.seam.example.projectmanager2;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@Entity
@Name("account")
@Scope(SESSION)
@Table(name="AddProject")
public class Account implements Serializable {
private String name;
private String description;
private String due;
private String budget;
private String people;
public Account(String name)
{
this.name = name;
}
public Account(){}
@Id
@Length(min=4, max=15)
@Pattern(regex="^\\w*$", message="not a valid name")
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
@NotNull
public String getDue()
{
return due;
}
public void setDue(String due)
{
this.due = due;
}
public String getBudget()
{
return budget;
}
public void setBudget(String budget)
{
this.budget = budget;
}
public String getPeople()
{
return people;
}
public void setPeople(String people)
{
this.people = people;
}
@Override
public String toString()
{
return "Account(" + name + ")";
}
}
addproject.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:a="http://richfaces.org/a4j">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="stylesheet/bcolor.css" type="text/css" rel="stylesheet"/>
<title>Project Management</title>
</head>
<body class="bccolor">
<h:form>
<ui:include src="dropdown.xhtml"></ui:include>
<h:messages globalOnly="true" styleClass="message"/>
<s:validateAll>
<f:facet name="aroundInvalidField">
<s:span styleClass="errors"/>
</f:facet>
<f:facet name="afterInvalidField">
<s:span> <s:message/></s:span>
</f:facet>
<f:facet name="header">Add Project</f:facet>
<table class="centeralign" border="1px" align="center">
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Name</td>
<td>
<s:decorate><h:inputText id="name" value="#{account.name}" required="true"/></s:decorate>
</td>
</tr>
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Discription</td>
<td>
<s:decorate><h:inputTextarea value="#{account.description}" id="description" required="true"/> </s:decorate>
</td>
</tr>
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Due</td>
<td>
<s:decorate><h:inputText id="due" value="#{account.due}" required="true"/></s:decorate>
</td>
</tr>
<tr style=" height : 25px;">
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Budget</td>
<td>
<s:decorate><h:inputText id="budget" value="#{account.budget}" required="true"/></s:decorate>
</td>
</tr>
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">People</td>
<td>
<s:decorate><h:inputText id="people" value="#{account.people}" required="true"/></s:decorate>
</td>
</tr>
</table>
<div class="buttonBox " align="center">
<h:commandButton id="register" value="Add" action="#{accountInformation.register}"/>
<s:button id="cancel" value="Cancel" view="/loginpage.xhtml"/>
</div>
</s:validateAll>
</h:form>
</body>
</html>
Please look at below code,
AccountAction.java
import static javax.persistence.PersistenceContextType.EXTENDED;
import static org.jboss.seam.ScopeType.EVENT;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.core.Events;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;
@Stateful
@Scope(EVENT)
@Name("accountInformation")
public class AccountAction implements AccountInformation
{
@In
private Account account;
@PersistenceContext
private EntityManager em;
@In
private FacesMessages facesMessages;
private String verify;
private boolean registered;
public void register()
{
if ( account.getName().equals(verify) )
{
List existing = em.createQuery("select name from Account where name=#{account.name} ")
.getResultList();
if (existing.size()==0)
{
em.persist(account);
facesMessages.add("Successfully registered as #{account.name}");
registered = true;
}
else
{
facesMessages.addToControl("name", "Name #{account.name} already exists");
}
}
}
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() {}
}
AccountInformation.java
import javax.ejb.Local;
@Local
public interface AccountInformation
{
public void register();
public void invalid();
public String getVerify();
public void setVerify(String verify);
public boolean isRegistered();
public void destroy();
}
Account.java
package org.seam.example.projectmanager2;
import static org.jboss.seam.ScopeType.SESSION;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@Entity
@Name("account")
@Scope(SESSION)
@Table(name="AddProject")
public class Account implements Serializable {
private String name;
private String description;
private String due;
private String budget;
private String people;
public Account(String name)
{
this.name = name;
}
public Account(){}
@Id
@Length(min=4, max=15)
@Pattern(regex="^\\w*$", message="not a valid name")
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
@NotNull
public String getDue()
{
return due;
}
public void setDue(String due)
{
this.due = due;
}
public String getBudget()
{
return budget;
}
public void setBudget(String budget)
{
this.budget = budget;
}
public String getPeople()
{
return people;
}
public void setPeople(String people)
{
this.people = people;
}
@Override
public String toString()
{
return "Account(" + name + ")";
}
}
addproject.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:a="http://richfaces.org/a4j">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="stylesheet/bcolor.css" type="text/css" rel="stylesheet"/>
<title>Project Management</title>
</head>
<body class="bccolor">
<h:form>
<ui:include src="dropdown.xhtml"></ui:include>
<h:messages globalOnly="true" styleClass="message"/>
<s:validateAll>
<f:facet name="aroundInvalidField">
<s:span styleClass="errors"/>
</f:facet>
<f:facet name="afterInvalidField">
<s:span> <s:message/></s:span>
</f:facet>
<f:facet name="header">Add Project</f:facet>
<table class="centeralign" border="1px" align="center">
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Name</td>
<td>
<s:decorate><h:inputText id="name" value="#{account.name}" required="true"/></s:decorate>
</td>
</tr>
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Discription</td>
<td>
<s:decorate><h:inputTextarea value="#{account.description}" id="description" required="true"/> </s:decorate>
</td>
</tr>
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Due</td>
<td>
<s:decorate><h:inputText id="due" value="#{account.due}" required="true"/></s:decorate>
</td>
</tr>
<tr style=" height : 25px;">
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">Budget</td>
<td>
<s:decorate><h:inputText id="budget" value="#{account.budget}" required="true"/></s:decorate>
</td>
</tr>
<tr>
<td style="COLOR: #c0c0c0; FONT-SIZE: large;">People</td>
<td>
<s:decorate><h:inputText id="people" value="#{account.people}" required="true"/></s:decorate>
</td>
</tr>
</table>
<div class="buttonBox " align="center">
<h:commandButton id="register" value="Add" action="#{accountInformation.register}"/>
<s:button id="cancel" value="Cancel" view="/loginpage.xhtml"/>
</div>
</s:validateAll>
</h:form>
</body>
</html>