Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created August 23, 2015 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/e9a76a732920d55553e9 to your computer and use it in GitHub Desktop.
Save thjanssen/e9a76a732920d55553e9 to your computer and use it in GitHub Desktop.
// 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");
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
// 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);
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
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