/modules/eclipselink2-jpa2/src/queries-stored_procedures/java/oopex/eclipselink2/jpa2/queries/StoredProceduresMain.java
Java | 103 lines | 64 code | 9 blank | 30 comment | 2 complexity | 08db14e2d5a42da07938f660dae3d094 MD5 | raw file
- /* Copyright (c) 2009-2010, Frank Schwarz <frank.schwarz@buschmais.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms,
- * with or without modification, are permitted provided
- * that the following conditions are met:
- * * Redistributions of source code must retain the
- * above copyright notice, this list of conditions
- * and the following disclaimer.
- * * Redistributions in binary form must reproduce
- * the above copyright notice, this list of
- * conditions and the following disclaimer in the
- * documentation and/or other materials provided
- * with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- */
- package oopex.eclipselink2.jpa2.queries;
-
- import java.sql.Types;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
-
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- import javax.persistence.Query;
-
- import oopex.eclipselink2.jpa2.queries.model.Person;
-
- import org.eclipse.persistence.jpa.JpaEntityManager;
- import org.eclipse.persistence.queries.ReadAllQuery;
- import org.eclipse.persistence.queries.StoredProcedureCall;
- import org.eclipse.persistence.sessions.Session;
-
- public class StoredProceduresMain {
-
- public static void main(String[] args) {
-
- EntityManagerFactory entityManagerFactory = Persistence
- .createEntityManagerFactory("default");
- try {
- System.out.println("*** query with JPQL ***");
- querywithjpql(entityManagerFactory);
- System.out.println("*** query with native api ***");
- querywithnativeapi(entityManagerFactory);
- } finally {
- entityManagerFactory.close();
- System.out.println("*** finished ***");
- }
- }
-
- @SuppressWarnings("unchecked")
- public static void querywithjpql(EntityManagerFactory entityManagerFactory) {
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- try {
- Query nativeQuery = entityManager.createNamedQuery("PersonList");
- nativeQuery.setParameter(1, "-2");
- List<Person> resultList = nativeQuery.getResultList();
- for (Person person : resultList) {
- System.out.println("found: " + person);
- }
- } finally {
- entityManager.close();
- }
- }
-
- @SuppressWarnings("unchecked")
- public static void querywithnativeapi(EntityManagerFactory entityManagerFactory) {
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- final Session session = entityManager.unwrap(JpaEntityManager.class).getSession();
- try {
- ReadAllQuery query = new ReadAllQuery(Person.class);
- StoredProcedureCall call = new StoredProcedureCall();
- call.addUnamedArgument("param", Types.INTEGER);
- call.setProcedureName("PERSON_LIST");
- query.setCall(call);
- query.addArgument("param");
- List<Object> parameters = new ArrayList<Object>();
- parameters.add(Integer.valueOf(-2));
- Collection<Person> collection = (Collection<Person>) session.executeQuery(query, parameters);
- for (Person person : collection) {
- System.out.println("found: " + person);
- }
- } finally {
- entityManager.close();
- }
- }
- }