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.

[6970 byte] By [BarmyYeTTia] at [2007-10-3 2:51:46]
# 1

Could some one confirm if JBoss 4.0.4 GA supports Persistence and session/entity beans using the new 1.5 JDK; using injection that is.

I have read in a couple of places that it does not support injection yet. Just looking to have this confirmed as this is probably what is causing my problem.

O` and is it also correct that JBoss does not support EAR's?

Cheers

BarmyYeTTia at 2007-7-14 20:40:40 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi there,

I've had the same question as you have (does JBoss support injections?).

It seems that injection in JBoss are supported but only from EJB container, if you want to get acces to Session Bean from outside the container , eg. from standalone client injection doesn't work.

You need then use something like this

ctx = new InitialContext();

testEntity = (EJBTestSessionRemote)ctx.lookup("DBTestOne/EJBTestSessionBean/remote");

instead of @EJB EJBTestSessionRemote sb;

The path in ctx.lookup() is specific to JBoss, to obtain in its best to see under JBoss console in JNDI Namespaces and see how looks path to your bean.

That is all I can tell you, it would be good if anyone comment this post, if I am right.

Experiments etc. :)

Good luck

Taras

taras_staza at 2007-7-14 20:40:40 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...