JBoss,J2EE, EJB3 + Persistence newbie
Hi all,
Have recently started experimenting with persistence, got it working fine under J2SE + Tomcat 5.
I am now looking to use it within J2EE. bearing in mind I have not realy done any J2EE work before, only JSP + servlets, I am having problems getting it running.
Firstly I am learning using two books.. Pro EJB 3 from Apress and JBoss at work from O`Reilly. The first of these is what got me up and running with persistence originally... anyway....
I am using JBuilder 2006, and too be honest not too sure how it should be setup for creating the required archives. at the moment i have it set to start JBoss when I build the project, it also creates a WAR file and a SAR file which it passes over. JNDI is setup and working ok.
The problem I am having is that the server is not recognising my session bean, it just returns null. The code I have pretty much copied from the book. it is using dependency injection to acquire the entity manager.
couple of my classes...
package kate;
import javax.persistence.EntityManager;
publicinterface UserService{
public User createUser(String login, String password, String access);
public User findUser(String login, String password);
public User findUser(int id);
public EntityManager getEntityManager();
publicvoid removeUser(int id);
}
package kate;
import javax.ejb.Stateless;
import javax.persistence.*;
/* Persistence access methods for user login*/
@Stateless
publicclass UserServiceBeanimplements UserService{
@PersistenceContext(unitName="KateEntityService")
protected EntityManager em;
public EntityManager getEntityManager(){
return em;
}
public User createUser(String login, String password, String access){
User user =new User();
// user.id is not required, as is autoincrement in db.
user.setLogin(login);
user.setPassword(password);
user.setAccess(access);
this.getEntityManager().persist(user);
return user;
}
publicvoid removeUser(int id){
User user = findUser(id);
if(user !=null){
this.getEntityManager().remove(user);
}
}
public User findUser(int id){
return this.getEntityManager().find(User.class,id);
}
public User findUser(String login, String password){
Query query = this.getEntityManager().createQuery("SELECT u FROM User u "+
"WHERE u.login = ?1 AND "+
"u.password = ?2")
.setParameter(1,login)
.setParameter(2,password);
return (User) query.getSingleResult();
}
}
package kate;
import javax.persistence.*;
import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;
import javax.ejb.EJB;
/*Retrieves the business logic(user details) from database,
this information is then transfered into a UserBean object
and returned to the calling class */
publicclass LoginMethod{
@EJB UserService bean;//service;
//EntityManagerFactory emf;
//EntityManager em;
private String username;
private String password;
public LoginMethod(){
/*
emf = Persistence.createEntityManagerFactory("KateEntityService");
em = emf.createEntityManager();
service = new UserServiceBean(em);
*/
}
public User getUser(String username, String password){
if(bean ==null){
System.out.println("bean = -NULL");
}
User user = bean.findUser(username,password);
//em.close();
return user;
}
}
Kate.war structure:
|
+css
+images
+META-INF
+tiles
+WEB-INF
| +classes
| | +kate
| | | UserService.class
| | | User.class
| | | ...
| | +META-INF
| | | persistence.xml
| | ApplicationResources.properties
| +lib
| | jboss-ejb3x.jar
| | toplink-essentials-agent.jar
| | toplink-essentials.jar
| | ...
| jboss-web.xml
| struts...
Login.jsp
Does this look ok?
Anyone know why JBoss is not picking up my session bean?
Also can anyone point me in the right direction of some sort of `simple` tutorial for packaging structure.
Sorry for all the questions.

