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
This commit is contained in:
Romain Manni-Bucau 2015-11-27 15:52:13 +00:00
parent 19c6b4130d
commit 715b9b086f
2 changed files with 72 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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 <T> TypedQuery<T> createQuery(String query, Class<T> resultClass) {
checkTuple(resultClass);
return createQuery(query).setResultClass(resultClass);
}
private <T> void checkTuple(Class<T> 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 <T> TypedQuery<T> createNamedQuery(String name, Class<T> 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);
}