Forked from thjanssen/CallNamedStoredProcedureQuery.java
Created
August 11, 2018 19:23
-
-
Save dinhchuong7913/d52c9551409b30b1da37bb898fc1d1f8 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
| StoredProcedureQuery q = this.em.createNamedStoredProcedureQuery(“getReviews”); | |
| q.setParameter(2, b.getId()); | |
| List<Review> reviews = q.getResultList(); |
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
| em.createQuery(“SELECT a FROM Author a WHERE a.id = function(‘calculate’, 1, 2)“, Author.class).getSingleResult(); |
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
| em.createQuery(“SELECT b.title, p.name FROM Book b JOIN b.publisher p”).getResultList(); |
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
| em.createQuery(“SELECT b.title, b.publisher.name FROM Book b”).getResultList(); |
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
| em.createQuery(“SELECT a FROM Author a”).setMaxResults(10).getResultList(); |
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
| SELECT * FROM author LIMIT 10 |
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
| @NamedStoredProcedureQuery( | |
| name = “getReviews”, | |
| procedureName = “get_reviews”, | |
| resultClasses = Review.class, | |
| parameters = { | |
| @StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, type = void.class), | |
| @StoredProcedureParameter(mode = ParameterMode.IN, type = Long.class) | |
| } | |
| ) |
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
| List<BookPublisherValue> bookPublisherValues = em.createQuery(“SELECT new org.thoughts.on.java.model.BookPublisherValue(b.title, b.publisher.name) FROM Book b”,BookPublisherValue.class).getResultList(); |
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
| List<Object[]> authorNames = em.createQuery(“SELECT a.firstName, a.lastName FROM Author a”).getResultList(); |
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
| em.createQuery(“SELECT a, count(b) FROM Author a JOIN a.books b GROUP BY a”).getResultList(); |
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
| em.createQuery(“SELECT a FROM Author a ORDER BY a.lastName”).getResultList(); |
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
| Query q = em.createQuery(“SELECT a FROM Author a WHERE a.id IN (SELECT s.authorId FROM SpecialAuthors s)”); |
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
| Query q = em.createQuery(“SELECT a FROM Author a WHERE a.id = :id”); |
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
| em.createQuery(“SELECT a, count(b) FROM Author a JOIN a.books b GROUP BY a”).getResultList(); |
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(“calculate”); | |
| query.registerStoredProcedureParameter(“x”, Double.class, ParameterMode.IN); | |
| query.registerStoredProcedureParameter(“y”, Double.class, ParameterMode.IN); | |
| query.registerStoredProcedureParameter(“sum”, Double.class, ParameterMode.OUT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment