Home > Java, Persistence > How to call a stored procedure and map resultset to entity with Derby and EclipseLink

How to call a stored procedure and map resultset to entity with Derby and EclipseLink

December 26, 2010 Leave a comment Go to comments

Assume you have a stored procedure in your Derby/JavaDB instance with following signature:

APP.PICK_AIRLINES(AIRLINE CHAR(2))

This stored procedure returns a resultset that you want to map with your Entity class, say, Airline.

Firstly I should let you know that, till date, Apache Derby [my version is 10.5] does not support Named Parameter in Callable Statements.

So, we cannot simply use @NamedStoredProcedureQuery Annotation with @StoredProcedureParameter , rather we need to use StoredProcedureCall method.

Here is the sample:

StoredProcedureCall storedProcedureCall = new StoredProcedureCall();
storedProcedureCall.setProcedureName("APP.PICK_AIRLINES");
storedProcedureCall.addUnamedArgumentValue("US");
ReadAllQuery readAllQuery = new ReadAllQuery(Airline.class,
storedProcedureCall);
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("aa");
@SuppressWarnings("unchecked")
List airLines = (List) getSession().executeQuery(readAllQuery);
for (Iterator iterator = airLines.iterator(); iterator
.hasNext();) {
Airline airline = (Airline) iterator.next();
//Now you have your Airline Entity
}

 

Points to Note:

1.        “UnamedArgumentValue” to set the parameter value at line 3 as Derby/JavaDB  does not support Named Parameter in Callable Statements

2.       At line 4 We are passing the Entity Type we are expecting to get populated.

This document could also be helpful if you are getting error message like:

1.   Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered “=” at line 1, column

Advertisement
Categories: Java, Persistence
  1. January 9, 2011 at 1:15 pm | #1

    I’ve been following your blog since you started. You have made amazing progress. This site is an inspiration for all pursuing a long transition versus the big chop.

    - Rob

    • Puspendu Banerjee
      January 17, 2011 at 10:46 pm | #2

      Thanks Rob.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.