PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/eclipselink2-jpa2/src/queries-stored_procedures/java/oopex/eclipselink2/jpa2/queries/StoredProceduresMain.java

http://oopex.googlecode.com/
Java | 103 lines | 64 code | 9 blank | 30 comment | 2 complexity | 08db14e2d5a42da07938f660dae3d094 MD5 | raw file
  1. /* Copyright (c) 2009-2010, Frank Schwarz <frank.schwarz@buschmais.com>
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms,
  5. * with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. * * Redistributions of source code must retain the
  8. * above copyright notice, this list of conditions
  9. * and the following disclaimer.
  10. * * Redistributions in binary form must reproduce
  11. * the above copyright notice, this list of
  12. * conditions and the following disclaimer in the
  13. * documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  17. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  29. * DAMAGE.
  30. */
  31. package oopex.eclipselink2.jpa2.queries;
  32. import java.sql.Types;
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.List;
  36. import javax.persistence.EntityManager;
  37. import javax.persistence.EntityManagerFactory;
  38. import javax.persistence.Persistence;
  39. import javax.persistence.Query;
  40. import oopex.eclipselink2.jpa2.queries.model.Person;
  41. import org.eclipse.persistence.jpa.JpaEntityManager;
  42. import org.eclipse.persistence.queries.ReadAllQuery;
  43. import org.eclipse.persistence.queries.StoredProcedureCall;
  44. import org.eclipse.persistence.sessions.Session;
  45. public class StoredProceduresMain {
  46. public static void main(String[] args) {
  47. EntityManagerFactory entityManagerFactory = Persistence
  48. .createEntityManagerFactory("default");
  49. try {
  50. System.out.println("*** query with JPQL ***");
  51. querywithjpql(entityManagerFactory);
  52. System.out.println("*** query with native api ***");
  53. querywithnativeapi(entityManagerFactory);
  54. } finally {
  55. entityManagerFactory.close();
  56. System.out.println("*** finished ***");
  57. }
  58. }
  59. @SuppressWarnings("unchecked")
  60. public static void querywithjpql(EntityManagerFactory entityManagerFactory) {
  61. EntityManager entityManager = entityManagerFactory.createEntityManager();
  62. try {
  63. Query nativeQuery = entityManager.createNamedQuery("PersonList");
  64. nativeQuery.setParameter(1, "-2");
  65. List<Person> resultList = nativeQuery.getResultList();
  66. for (Person person : resultList) {
  67. System.out.println("found: " + person);
  68. }
  69. } finally {
  70. entityManager.close();
  71. }
  72. }
  73. @SuppressWarnings("unchecked")
  74. public static void querywithnativeapi(EntityManagerFactory entityManagerFactory) {
  75. EntityManager entityManager = entityManagerFactory.createEntityManager();
  76. final Session session = entityManager.unwrap(JpaEntityManager.class).getSession();
  77. try {
  78. ReadAllQuery query = new ReadAllQuery(Person.class);
  79. StoredProcedureCall call = new StoredProcedureCall();
  80. call.addUnamedArgument("param", Types.INTEGER);
  81. call.setProcedureName("PERSON_LIST");
  82. query.setCall(call);
  83. query.addArgument("param");
  84. List<Object> parameters = new ArrayList<Object>();
  85. parameters.add(Integer.valueOf(-2));
  86. Collection<Person> collection = (Collection<Person>) session.executeQuery(query, parameters);
  87. for (Person person : collection) {
  88. System.out.println("found: " + person);
  89. }
  90. } finally {
  91. entityManager.close();
  92. }
  93. }
  94. }