A CDI Extension to call stored procedures or database functions declaratively using hibernate.
Feel free to use this library as you wish, make sure to quote the LGPL in all used sources.
Include Maven dependency on your pom.xml
<dependency>
<groupId>es.indaba</groupId>
<artifactId>DbCallCDI</artifactId>
<version>1.8</version>
</dependency>
Define your CDI Bean as an interface. The implementation will be provided by the CDI extension:
@ApplicationScoped
public interface DBTester {
@StoredProcedure("CALL echoProc(?,?)")
@StoredProcedureResult({ @FieldResult(name = "value", position = 2) })
@DatabaseCall
public ProcedureResult<String> callEchoAsProcedure(@StoredProcedureParameter(1) String name) throws Exception;
}
The extension interprets the following annotations in order to build a call to a database stored procedures.
- DatabaseCall defines that this method should be processed as a stored procedure call
- StoredProcedure defines the database call expression
- StoredProcedureResult defines the mapping of the output values retrieved from the stored procedure call and the returning object properties
- StoredProcedureParameter identifies the stored procedure input parameters
Executing db call:
The interface is injected as a normal CDI bean
@Inject
private DBTester dbTester;
...
ProcedureResult<String> result = dbTester.callEchoAsProcedure(testVal);
The returning type looks like this
public class ProcedureResult<T> {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
@ApplicationScoped
public interface DBTester {
@StoredProcedure("CALL echoProc(?,?)")
@StoredProcedureResult({ @FieldResult(name = "value", position = 2) })
@DatabaseCall(qualifier=MyDatabase.class)
public ProcedureResult<String> callEchoAsProcedure(@StoredProcedureParameter(1) String name) throws Exception;
}
Check tests for detailed use.
- JPA 2.1 Stored Procedure Support - http://docs.oracle.com/javaee/7/api/javax/persistence/StoredProcedureQuery.html
- JDBC CallableStatement - https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html
- CDI Extension - https://docs.jboss.org/weld/reference/latest/en-US/html/extend.html
- DeltaSpike - https://deltaspike.apache.org/index.html
Pull requests are welcomed!!