Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Getting Session Using JpaHelper.getEntityManager
Getting Session Using JpaHelper.getEntityManager [message #547795] Mon, 19 July 2010 11:56 Go to next message
Shelli Orton is currently offline Shelli Orton
Messages: 76
Registered: September 2009
Member
Hi,

I am using EclipseLink as my JPA provider and have a need to execute an Oracle stored function. I have written the following method in my GenericServiceBean class:
public String executeStoredFunctionWithNamedArguments(String functionName,
            Map<String, String> namedArguments)
    {        
        Session session = JpaHelper.getEntityManager(em).getSession();
        
        StoredFunctionCall functionCall = new StoredFunctionCall();
        functionCall.setProcedureName(functionName);        
        functionCall.setResult("RESULT", String.class);
        
        ValueReadQuery query = new ValueReadQuery();
        query.setCall(functionCall);
        
        List<String> values = new ArrayList<String>();
        
        for (String key : namedArguments.keySet())
        {
            query.addArgument(key);
            values.add(namedArguments.get(key));
        }
        
        String status = (String)session.executeQuery(query, values);
        
        return status;
    }


When the code executes, I get a NPE on the session.executeQuery. I have confirmed that the em is not null (is being injected properly).

Can someone tell me what I'm doing wrong?

Thanks!
Re: Getting Session Using JpaHelper.getEntityManager [message #547830 is a reply to message #547795] Mon, 19 July 2010 13:44 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1018
Registered: July 2009
Senior Member
Hello,

EntityManagerImpl's getSession "will return a Session outside of a transaction and null within a transaction". What you probably want to use instead is getServerSession() or better, getUnitOfWork() for transactional data.

Best Regards,
Chris
Re: Getting Session Using JpaHelper.getEntityManager [message #547848 is a reply to message #547830] Mon, 19 July 2010 15:22 Go to previous messageGo to next message
Shelli Orton is currently offline Shelli Orton
Messages: 76
Registered: September 2009
Member
Hi,

Thanks for the reply. I had been experimenting and had switched to using getServerSession and now the session variable is not null and the function is being executed.

However, I am now trying to figure out why I am getting this error:
[#|2010-07-19T11:38:43.406-0600|WARNING|glassfish3.0.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=26;_ThreadName=Thread-1;|StandardWrapperValve[Test Service]: PWC1406: Servlet.service() for servlet Test Service threw exception
Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00306: wrong number or types of arguments in call to 'MY_FUNC'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

Error Code: 6550
Call: BEGIN ? := MY_FUNC(); END;
	bind => [=> RESULT]
Query: ValueReadQuery()
	at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
...


MY_FUNC takes 4 varchar2 IN variables which appear to be being set in the query when I step through the code (the for loop in the above method). However, they are not being displayed in the log so am not sure if they are actually being set or not.

I based my code on the StoredFunctionCall example from this page: http://wiki.eclipse.org/Using_Basic_Query_API_%28ELUG%29#Usi ng_a_StoredFunctionCall

Am I doing the right thing for setting the arguments?

Thanks again!
Re: Getting Session Using JpaHelper.getEntityManager [message #547849 is a reply to message #547848] Mon, 19 July 2010 15:53 Go to previous messageGo to next message
Shelli Orton is currently offline Shelli Orton
Messages: 76
Registered: September 2009
Member
I have also tried this in my method:
        ValueReadQuery query = new ValueReadQuery();
        query.setCall(functionCall);
        
        for (String key : namedArguments.keySet())
        {
            query.addArgument(key);
            query.addArgumentValue(namedArguments.get(key));
        }
                
        String status = (String)session.executeQuery(query);
        
        return status;

but get the same error as above.
Re: Getting Session Using JpaHelper.getEntityManager [message #547860 is a reply to message #547849] Mon, 19 July 2010 16:50 Go to previous message
Shelli Orton is currently offline Shelli Orton
Messages: 76
Registered: September 2009
Member
I have figured out a way to make this work. For future reference here is my final method:

    public String executeStoredFunctionWithNamedArguments(String functionName,
            LinkedHashMap<String, String> namedArguments)
    {
        Session session = JpaHelper.getEntityManager(em).getServerSession();
        
        StoredFunctionCall functionCall = new StoredFunctionCall();
        functionCall.setProcedureName(functionName);        
        functionCall.setResult("RESULT", String.class);
                
        for (String key : namedArguments.keySet())
        {
            functionCall.addNamedArgumentValue(key, namedArguments.get(key));
        }
        
        ValueReadQuery query = new ValueReadQuery();
        query.setCall(functionCall);

        String status = (String)session.executeQuery(query);
        
        return status;
    }
Previous Topic:Building object hierarchy causes synchronization
Next Topic:new jpa project user library requires javax.persistence.entity
Goto Forum:
  


Current Time: Sat Aug 30 16:39:01 EDT 2014

Powered by FUDForum. Page generated in 0.02064 seconds