Created
August 23, 2015 03:23
-
-
Save thjanssen/e9a76a732920d55553e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set input parameter | |
query.setParameter("x", 1.23d); | |
query.setParameter("y", 4.56d); | |
// call the stored procedure and get the result | |
query.execute(); | |
Double sum = (Double) query.getOutputParameterValue("sum"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION calculate( | |
IN x double precision, | |
IN y double precision, | |
OUT sum double precision) | |
RETURNS double precision AS | |
$BODY$ | |
BEGIN | |
sum = x + y; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define the stored procedure | |
StoredProcedureQuery query = this.em.createStoredProcedureQuery("calculate"); | |
query.registerStoredProcedureParameter("x", Double.class, ParameterMode.IN); | |
query.registerStoredProcedureParameter("y", Double.class, ParameterMode.IN); | |
query.registerStoredProcedureParameter("sum", Double.class, ParameterMode.OUT); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION get_reviews(bookid bigint) | |
RETURNS refcursor AS | |
$BODY$ | |
DECLARE | |
reviews refcursor; -- Declare cursor variables | |
BEGIN | |
OPEN reviews FOR SELECT id, comment, rating, version, book_id FROM review WHERE book_id = bookId; | |
RETURN reviews; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StoredProcedureQuery query = this.em.createStoredProcedureQuery("get_books", Book.class); | |
query.registerStoredProcedureParameter(1, void.class, ParameterMode.REF_CURSOR); | |
query.execute(); | |
List<Book> books = (List<Book>) query.getResultList(); | |
for (Book b : books) { | |
// do something useful | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment