Hello,
I want to write a simple stateful session bean, but I'm surprised, that it doesn't work.
I'm totally confused, as some examples in the internet base on annotations and others not...
I've got the following RemoteInterface:
Code:
@Remote
public interface ShopUserSessionRemote extends Serializable {
public ShopUser getUser();
public void setUser(ShopUser shopUser);
}
Here my implementation:
Code:
@Stateful(name="userSession")
public class ShopUserSessionImpl implements ShopUserSessionRemote{
/**
*
*/
private static final long serialVersionUID = 6717871409083582877L;
@Id
@GeneratedValue
private Long id;
@Column(name="user")
public ShopUser user;
public ShopUser getUser() {
return user;
}
public void setUser(ShopUser user) {
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
So my first questions:
Do I need an id for a stateful session bean? If yes, should it be an @GeneratedValue?
Do I need a column for the user? @Column(name="user")
In my Login-Servlet I'm getting my Session via lookup:
Code:
Long id = Long.parseLong(request.getParameter("userId"));
EntityManager entityManager = entityManagerFactory.createEntityManager();
ShopUser shopUser = entityManager.find(ShopUser.class, id);
try {
Context initContext = new InitialContext();
ShopUserSessionRemote shopUserSession = (ShopUserSessionRemote) initContext.lookup("Shop/userSession/remote");
shopUserSession.setUser(shopUser);
} catch(Exception e){
throw new ServletException(e);
}
After redirecting to the next page, I've lost this information in my next lookup:
Code:
Context initContext = new InitialContext();
ShopUserSessionRemote shopUserSession = (ShopUserSessionRemote) initContext.lookup("Shop/userSession/remote");
ShopUser shopUser = shopUserSession.getUser();
How do I acces a session bean? via lookup?
I saw an example, where someone saved the object in a JSP session, but I think, that hardly makes sense. Does it?
I've got a hibernate.cfg.xml, although I think I don't need it. Do I?
Quote:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="eu.glotzich.j2ee.ejb.ShopUser"/>
</session-factory>
</hibernate-configuration>
I set it up for an example in the Hibernate reference:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
EntityManager entityManager = entityManagerFactory.createEntityManager();
ShopUser shopUser = entityManager.find(ShopUser.class, id);
ShopUserSession shopUserSession = new ShopUserSession(); // ShopUserSession is a different class
shopUserSession.setUser(shopUser);
session.beginTransaction();
session.save(shopUserSession);
session.getTransaction().commit();
When should I use this kind of session? It seems to me just like a central session management, if you use clustering, as every
session information is stored in the same 'database'. Correct?
But I would still have to save the id in my JSP session or somewhere else, don't I?
Sorry for the whole bunch of questions :)
The most important question for me is, why doesn't it work? ^^
Thank you for your help,
glotzich