- This topic has 10 replies, 4 voices, and was last updated 15 years, 3 months ago by Brian Fernandes.
-
AuthorPosts
-
humbfdljrMemberHi, I created an EJB Project, after that I did the EJB 3 reverse engineering of the table user (id : integer [PK], FirstName : varchar, LastName : varchar, Username : varchar, Password : varchar, DataCreated : bigint).
I created the java class testeejb:
package com.genuitec; public class testeejb { public static void main(String[] args) { UserFacade uf = new UserFacade(); User u = new User(); u.setFirstName("joao"); u.setLastName("silva"); u.setUsername("jsilva"); u.setPassword("1234567"); u.setId(3); u.setDataCreated(new Long(100)); uf.save(u); } }
When I run this project I got these errors:
23/06/2009 17:12:07 com.genuitec.LogUtil log
SEVERE: save failed
java.lang.NullPointerException
at com.genuitec.UserFacade.save(UserFacade.java:41)
at com.genuitec.testeejb.main(testeejb.java:14)
Exception in thread “main” java.lang.NullPointerException
at com.genuitec.UserFacade.save(UserFacade.java:41)
at com.genuitec.testeejb.main(testeejb.java:14)But I’ve no idea why I got NullPointer exception because I setted all variables from the user table from the database.
Here is exactly where I got the NullPointer Exception:
public void save(User entity) { LogUtil.log("saving User instance", Level.INFO, null); try { entityManager.persist(entity); //HERE I GOT THE ERROR LogUtil.log("save successful", Level.INFO, null); } catch (RuntimeException re) { LogUtil.log("save failed", Level.SEVERE, re); throw re; } }
Someone could help me? Thanks.
support-joyMemberhumbfdljr,
I am able to reproduce this. I have raised PR for the dev team. A dev team member will get back to you.
humbfdljrMemberOk, thanks. Just a few more details:
* I’m using the postgresql database (it’s working fine, because I already tested with a hibernate project).
* This error is happening not only in the save, it happens every time I use a method of EntityManager (entity = entityManager.getReference(User.class, entity.getId()) from delete for example).
* Debugging my code I discovered entityManager come as null in the line that I wrote “//HERE I GOT THE ERROR” at my previous post.
* When I create the ejb project I’m not sure what I should put in “JNDI Data Source”, I’m putting “java:/postgres”
* I tryed this project in myeclipse 7.1 and 7.5 with the same error
support-joyMemberhumbfdljr,
Thank you for the additional notes. Yes, my initial investigation did reveal that reverse engineering is not creating EntityManager object. I have added these to the PR. Can you create an entityManager object in your User class and see if helps. You might need to download the relevant jar file and add it as a reference lib to the project. This issue is currently under investigation.
Thank you for your patience.
humbfdljrMemberI created this object at my User class:
@Entity @Table(name = "user", schema = "public") public class User extends AbstractUser implements java.io.Serializable { [color=red] private EntityManager entityManager = null; @PersistenceContext public void setEntityManager(EntityManager em) { this.entityManager = em; }[/color] // Constructors /** default constructor */ public User() { } /** full constructor */ public User(Integer id, String username, String password, String firstName, String lastName) { super(id, username, password, firstName, lastName); } }
But I’m still gotting the same error.
Brian FernandesModeratorhumbfdljr,
What server are you deploying your application to? Are you deploying to an application server which supports EJB3? If you deploy this to a barebones Tomcat installation or the embedded MyEclipse Tomcat for instance, this will not work. You need a server like Glassfish which has EJB3 support or you will need to add an EJB3 container like EasyBeans to Tomcat before you can get this working.
The entityManager field you see is assigned at runtime using dependency injection (which is why it has the @PersistenceContext annotation). The fact that it’s not happening indicates that you are either deploying to a server which does not support EJB3 or there is some other configuration problem with your application.
Do you really want EJB3 support or do you just need the persistence? If you only need persistence support, you can create a JPA application instead which can be deployed to almost all servers, the EJB3 container is not required. Note that EJB3 uses JPA for persistence so most of the POJOs will look similar.
Hope this helps.
humbfdljrMember@Support-Brian wrote:
humbfdljr,
What server are you deploying your application to? Are you deploying to an application server which supports EJB3? If you deploy this to a barebones Tomcat installation or the embedded MyEclipse Tomcat for instance, this will not work. You need a server like Glassfish which has EJB3 support or you will need to add an EJB3 container like EasyBeans to Tomcat before you can get this working.
The entityManager field you see is assigned at runtime using dependency injection (which is why it has the @PersistenceContext annotation). The fact that it’s not happening indicates that you are either deploying to a server which does not support EJB3 or there is some other configuration problem with your application.
Do you really want EJB3 support or do you just need the persistence? If you only need persistence support, you can create a JPA application instead which can be deployed to almost all servers, the EJB3 container is not required. Note that EJB3 uses JPA for persistence so most of the POJOs will look similar.
Hope this helps.
Actually, I’ll need to use EJB 3.0 in my project, but I’m still learning it.
My App Server is JBoss 5.1.
When I run a EJB project, I have to select run->Java Application, or I have to select run->MyEclipse Server Application?
rmcvayMemberServer App
humbfdljrMemberOk, that was my error, now I deployed my project with JBoss.
Now how can I test my program? How can I run it?
That’s just a EJB project, without Web, it’s possible test it in a browser?
support-joyMemberOnce you have deployed to JBoss, right click on your project and select run as > MyEclipse Server Application and choose Jboss. You should be all set. If you are new to EJB, I would recommend you to go through our education material on EJB here – http://www.myeclipseide.com/documentation/quickstarts/firstejb/
Brian FernandesModeratorPlease also read our Enterprise application tutorial here: http://www.myeclipseide.com/documentation/quickstarts/earprojects/
Also, back to your original problem, you need to make sure you get your UserFacade instance using the following code
InitialContext ctx = new InitialContext(); UserFacadeRemote fac = (UserFacadeRemote)ctx.lookup("my.package.UserFacadeRemote");
This will ensure that the entityManager field is assigned with a value using Dependency Injection (if you just create a facade using new UserFacade(), the injection will not happen). During RE, please enable the generation of a Remote Facade.
Hope this helps.
-
AuthorPosts