aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes NullPointerException returned usig a StoredProcedureCall Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "NullPointerException returned usig a StoredProcedureCall" Watch "NullPointerException returned usig a StoredProcedureCall" New topic
Author

NullPointerException returned usig a StoredProcedureCall

Jose Alvarez de Lara
Ranch Hand

Joined: May 10, 2008
Posts: 92
Hi,

I am developping a JPA, EJB and JSF 2.0 web app. I persist an entity PedidoUsuario and afetr it I want to get its PK in
the corresponding table ALMACEN.PEDIDO_USUARIO. To persist the entity I use an special EJB that calls another EJBs.

Here is the EJB,



The problem is in the method placeOrder(Usuario usuario, ShoppingCart cart) when I try to get the PK of the persisted PedidoUsuario.
For it I use an EJB that implements a StoredProcedureCall. Why? The reason why is that if I use JPQL, being the PK type of sequence
it returns me a wrong PK. So I guessed it could be resolved attacking the database directly.

Here is the EJB,



being the SP as follows,



But it fails. The EJB method ObtenerMaxId() returns a NullPointerException.

Maybe I am wrong in the code of the SP or maybe in the EJBs, I do not know.

Please help.

Jose Alvarez de Lara
Ranch Hand

Joined: May 10, 2008
Posts: 92
Hi,

I am newbe in this question so I need help.

Kind regards,
Jose
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Hi Jose,

So, you just want to persist an entity and get it's primary key? So you don't need the stored procedure, but just introduced it to test if it'll work?

What is the "StoredProcedure" class and "JpaEntityManager"?
What does 'pedidoUsuarioFacade.create(order); ' do?
How is your entity annotated?


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Jose Alvarez de Lara
Ranch Hand

Joined: May 10, 2008
Posts: 92
Hi Piotr,

First of all thanks for your interesting.

About your questios,

1)What is the "StoredProcedure" class and "JpaEntityManager"?

I took the code from another example that I have.

2)What does 'pedidoUsuarioFacade.create(order); ' do?

It persist an entity PedidoUsuario.

3)How is your entity annotated?

Here is the code snippet of the entities I manage in ManejadorPedido session bean,







I am not sure if this is the information you are asking me. If not let me know it.

Kind regards,
Jose
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Ok Jose, so you're using a sequence generator for your ID's. Could you test just some minimalist code if the entity is persisted, i.e.:If you invoke this code, you should see the automatically generated (using your sequence) ID printed in the console. If you don't than I guess it's something wrong with this sequence generator.

By the way - is this (assigning of an ID and reading it back) all what you want to achieve right now?

Cheers!
Jose Alvarez de Lara
Ranch Hand

Joined: May 10, 2008
Posts: 92
Hi Piotr,

You say,

>>By the way - is this (assigning of an ID and reading it back) all what you want to achieve right now?

Yes, that is all.


I do not understand why the sequence generator returns pu.getPedidousuario 106 value when the PK in the table Pedido_Usuario (ID_PEDIDOUSUARIO)
is 107 value. This is the reasson why I prefer to get it calling a SP or a Function. But even in those cases I get a NullPointer.

I am in conversation with James Sutherland from EclipseLink by the mailing list but all the help should be really appreciated.

In fact this is all I need to end my web app.

Kind regards,
Jose
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Jose, so on your console you've got printed:

1. NullPointerException
2. 106
3. 107
?

If you try to lookup this entity using EntityManager, i.e.:



The second time on the console - will it print an Exception, 106 or 107?

Do you mean that the Object representation of your database is inconsistent?
Jose Alvarez de Lara
Ranch Hand

Joined: May 10, 2008
Posts: 92
Hi,

Let's go on tomorrow. Now is 23:30 pm in Madrid (Spain) and I am a little bit tired.
I like your way of working. I think we can resolve this issue.

Thanks a lot,
Jose
Jose Alvarez de Lara
Ranch Hand

Joined: May 10, 2008
Posts: 92
Hi

It is happening something estrange in my Oracle database. I have to ways of getting the
ID_PEDIDOUSUARIO. One of them is as follows,


em.persist(pedidoUsuario);
em.flush();
return pedidoUsuario.getIdPedidousuario();


The other one is calling a StoredFunctionCall that gets the last Id inserted in the database.
The estrange thing is that in both cases I get a value one unit less than the real Id (PK).
I mean if the inserted Id (PK) has value of ā€˜nā€™ I get in both cases ā€˜n-1ā€™. And that is the
reason I am getting NullPointerException for the first inserting.

But one thing that I cannot understand is that if I execute the Function from inside an Oracle
management tool I get the correct value so there must be something wrong during the execution
of the EJBs.

Any suggestion about this issue should be really appreciated.

Kind regards,
Jose
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException returned usig a StoredProcedureCall
 
Similar Threads
MZs Notes - Pg 159
Mess with EntityManagers and EntityManagerFactory
EJB3 Null Pointer Exception
JPA and StoredProcedureCall
Problem with a shopping cart application using Stripes