From 715b9b086fba1d0214a4a820047240857aa27640 Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Fri, 27 Nov 2015 15:52:13 +0000 Subject: [PATCH] OPENJPA-2618 pevent Tuple to pass through in createXQuery methods since we dont support it yet git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1716891 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/query/TupleQueryTest.java | 61 +++++++++++++++++++ .../persistence/EntityManagerImpl.java | 12 +++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TupleQueryTest.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TupleQueryTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TupleQueryTest.java new file mode 100644 index 000000000..967a4bdc3 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TupleQueryTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.query; + +import junit.textui.TestRunner; +import org.apache.openjpa.persistence.simple.NamedEntity; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceException; +import javax.persistence.Tuple; + +public class TupleQueryTest + extends SingleEMFTestCase { + + public void setUp() { + setUp(NamedEntity.class); + + NamedEntity e = new NamedEntity(); + e.setName("e"); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(e); + em.getTransaction().commit(); + em.close(); + } + + public void testNormalQuery() { + EntityManager em = emf.createEntityManager(); + try { + em.createQuery("select e.name from named e", Tuple.class); + fail(); + } catch (final PersistenceException pe) { + // ok + } finally { + em.close(); + } + } + + public static void main(String[] args) { + TestRunner.run(TupleQueryTest.class); + } +} + diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java index cfa0f1d35..095796052 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java @@ -46,6 +46,7 @@ import javax.persistence.FlushModeType; import javax.persistence.LockModeType; import javax.persistence.PessimisticLockScope; import javax.persistence.Query; +import javax.persistence.Tuple; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.ParameterExpression; @@ -972,9 +973,16 @@ public class EntityManagerImpl @SuppressWarnings("unchecked") public TypedQuery createQuery(String query, Class resultClass) { + checkTuple(resultClass); return createQuery(query).setResultClass(resultClass); } - + + private void checkTuple(Class resultClass) { + if (Tuple.class == resultClass) { + throw new PersistenceException("Tuple is not a valid type", null, null, true); + } + } + public OpenJPAQuery createQuery(String query) { return createQuery(JPQLParser.LANG_JPQL, query); } @@ -1014,6 +1022,7 @@ public class EntityManagerImpl @SuppressWarnings("unchecked") public TypedQuery createNamedQuery(String name, Class resultClass) { + checkTuple(resultClass); return createNamedQuery(name).setResultClass(resultClass); } @@ -1055,6 +1064,7 @@ public class EntityManagerImpl } public OpenJPAQuery createNativeQuery(String query, Class cls) { + checkTuple(cls); return createNativeQuery(query).setResultClass(cls); }