PLEASE HELP!!! I am new to EJB3, and have been trying to develop a simple login application using
EJB, JPA, and
JSF. I keep getting the following error from
JBOSS:
12:38:33,062 ERROR [LazyInitializationException] could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:166)
at com.csw.ejb3.entity.User_$$_javassist_1.getPassword(User_$$_javassist_1.java)
at com.csw.web.util.SignInBean.getUser(SignInBean.java:50)
at com.csw.web.util.SignInBean.signIn(SignInBean.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.el.parser.AstValue.invoke(AstValue.java:131)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
at javax.faces.component.UICommand.broadcast(UICommand.java:383)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
My entity class is:
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User implements Serializable {
public static final
String FLAG_VALUE_NO = "N";
public static final String FLAG_VALUE_YES = "Y";
@Id
@Column(name="user_id")
private String user_id;
@Column(name="password")
private String password;
@Column(name="group_policy_name")
private String group_policy_name;
@Column(name="first_name")
private String first_name;
@Column(name="mi")
private String mi;
@Column(name="last_name")
private String last_name;
@Column(name="address1")
private String address1;
@Column(name="address2")
private String address2;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
@Column(name="zip")
private String zip;
@Column(name="phone1")
private String phone1;
@Column(name="phone2")
private String phone2;
@Column(name="extension")
private String extension;
@Column(name="title")
private String title;
@Column(name="disable_flag")
private String disable_flag;
@Column(name="remote_flag")
private String remote_flag;
@Column(name="entry_date")
private Date entry_date;
@Column(name="added_by")
private String added_by;
@Column(name="modify_date")
private Date modify_date;
@Column(name="modified_by")
private String modified_by;
public User(){
}
public User(String user_id,
String password,
String group_policy_name,
String first_name,
String mi,
String last_name,
String address1,
String address2,
String city,
String state,
String zip,
String phone1,
String phone2,
String extension,
String title,
String disable_flag,
String remote_flag,
Date entry_date,
String added_by,
Date modify_date,
String modified_by){
this.user_id = user_id;
this.password = password;
this.group_policy_name = group_policy_name;
this.first_name = first_name;
this.mi = mi;
this.last_name = last_name;
this.address1 = address1;
this.address2 = address2;
this.city = city;
this.state = state;
this.zip = zip;
this.phone1 = phone1;
this.phone2 = phone2;
this.extension = extension;
this.title = title;
this.disable_flag = disable_flag;
this.remote_flag = remote_flag;
this.entry_date = entry_date;
this.added_by = added_by;
this.modify_date = modify_date;
this.modified_by = modified_by;
}
public String getUser_id() {
return user_id;
}
public String getPassword() {
return password;
}
public String getGroup_policy_name() {
return group_policy_name;
}
public String getFirst_name() {
return first_name;
}
public String getMi() {
return mi;
}
public String getLast_name() {
return last_name;
}
public String getAddress1() {
return address1;
}
public String getAddress2() {
return address2;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZip() {
return zip;
}
public String getPhone1() {
return phone1;
}
public String getPhone2() {
return phone2;
}
public String getExtension() {
return extension;
}
public String getTitle() {
return title;
}
public String getDisable_flag() {
return disable_flag;
}
public String getRemote_flag() {
return remote_flag;
}
public Date getEntry_date() {
return entry_date;
}
public String getAdded_by() {
return added_by;
}
public Date getModify_date() {
return modify_date;
}
public String getModified_by() {
return modified_by;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public void setPassword(String password) {
this.password = password;
}
public void setGroup_policy_name(String group_policy_name) {
this.group_policy_name = group_policy_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public void setMi(String mi) {
this.mi = mi;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
public void setZip(String zip) {
this.zip = zip;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public void setExtension(String extension) {
this.extension = extension;
}
public void setTitle(String title) {
this.title = title;
}
public void setDisable_flag(String disable_flag) {
this.disable_flag = disable_flag;
}
public void setRemote_flag(String remote_flag) {
this.remote_flag = remote_flag;
}
public void setEntry_date(Date entry_date) {
this.entry_date = entry_date;
}
public void setAdded_by(String added_by) {
this.added_by = added_by;
}
public void setModify_date(Date modify_date) {
this.modify_date = modify_date;
}
public void setModified_by(String modified_by) {
this.modified_by = modified_by;
}
}
My dao classes are:
import java.util.List;
import java.io.Serializable;
import com.csw.ejb3.exception.DaoException;
public interface IGenericDao<T, PK extends Serializable> {
/** Retrieve all objects that were previously persisted to the database */
List<T> findAll() throws DaoException;
/** Retrieve an object that was previously persisted to the database using
* the indicated id as a primary key */
T find(PK id) throws DaoException;
/** Save the changes made to a persistent object. */
void create(T object) throws DaoException;
/** Update the changes made to an existing persistent object. */
void update(T object) throws DaoException;
/** Picks the appropriate method add(T object) or update(T object) to call by determining if
* this object currently exists in the database.*/
void save(T object) throws DaoException;
/** Remove an object from persistent storage in the database */
void delete(T object) throws DaoException;
}
import java.io.Serializable;
import java.util.List;
import java.lang.reflect.ParameterizedType;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import com.csw.ejb3.exception.DaoException;
public class GenericDao<T, PK extends Serializable> implements IGenericDao<T, PK> {
@PersistenceContext
private EntityManager em;
private Class<T> type;
@SuppressWarnings("unchecked")
public GenericDao(){
this.type = (Class<T>
((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void create(T object) throws DaoException{
try{
em.persist(object);
}
catch(Exception e){
throw new DaoException(e.getMessage(),e);
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void update(T object) throws DaoException{
try{
em.merge(object);
}
catch(Exception e){
throw new DaoException(e.getMessage(),e);
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void save(T object) throws DaoException{
try{
em.persist(object);
}
catch(Exception e){
throw new DaoException(e.getMessage(),e);
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void delete(T object) throws DaoException{
try{
em.remove(object);
}
catch(Exception e){
throw new DaoException(e.getMessage(),e);
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public T find(PK id) throws DaoException{
try{
return em.getReference(type, id);
}
catch(EntityNotFoundException e){
throw new DaoException(e.getMessage(),e);
}
catch(Exception e){
throw new DaoException(e.getMessage(),e);
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@SuppressWarnings("unchecked")
public List<T> findAll() throws DaoException{
try{
return (List<T>
em.createQuery("from " + type.getSimpleName()).getResultList();
}
catch(Exception e){
throw new DaoException(e.getMessage(),e);
}
}
import javax.ejb.Stateless;
import com.csw.ejb3.entity.User;
import com.csw.ejb3.session.stateless.GenericDao;
@Stateless(name = "UserDao")
public class UserDao extends GenericDao<User,String> implements IUserDaoLocal, IUserDaoRemote {
}
import javax.ejb.Remote;
import com.csw.ejb3.entity.User;
import com.csw.ejb3.session.stateless.IGenericDao;
@Remote
public interface IUserDaoRemote extends IGenericDao<User, String>{
}
import javax.ejb.Local;
import com.csw.ejb3.session.stateless.IGenericDao;
import com.csw.ejb3.entity.User;
@Local
public interface IUserDaoLocal extends IGenericDao<User,String>{
}
My JSF backing bean is:
import org.apache.log4j.Logger;
import com.csw.ejb3.entity.User;
import com.csw.ejb3.session.stateless.UserDao.IUserDaoRemote;
import com.csw.ejb3.util.EjbLookup;
import com.csw.web.exception.SecurityCheckException;
public class SignInBean {
private static Logger log = Logger.getLogger(SignInBean.class);
String user_id;
String password;
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String signIn(){
try {
User user = getUser();
signInUser(user);
return WebLookup.lookup().getPrivateNavigation().goDebtorInfoPage();
}
catch (SecurityCheckException e) {
log.warn("Login attempt denied.");
log.debug(e.getMessage(), e);
return null;
}
}
private User getUser() throws SecurityCheckException {
try {
IUserDaoRemote remote = EjbLookup.getUserDaoRemote();
String username = getUser_id().toUpperCase();
User user = remote.find(username);
if(user.getPassword().equalsIgnoreCase(password)) {
return user;
}
else {
throw new SecurityCheckException("Incorrect password.");
}
}
catch (Exception e) {
log.warn("Unknown error occurred checking credentials during login.");
log.debug("Username: " + user_id + ", Password: " + password);
ErrorUtil.addError("An unknown error occurred during login.");
throw new SecurityCheckException(e.getMessage(), e);
}
}
private void signInUser(User user) throws SecurityCheckException {
new SecurityManager(user);
}