From ebe06fdf352a63c3152874b3efdc4960108cf3b5 Mon Sep 17 00:00:00 2001 From: Heath Thomann Date: Thu, 31 May 2012 13:39:52 +0000 Subject: [PATCH] OPENJPA-2174: Applied Helen Xu's patch to trunk git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1344721 13f79535-47bb-0310-9956-ffa450edef68 --- .../MappedQueryResultObjectProvider.java | 13 +++- .../openjpa/jdbc/sql/ResultSetResult.java | 1 + .../criteria/results/ShipRate.java | 47 +++++++++++++ .../query/results/TestResultSetMapping.java | 66 +++++++++++++++++++ 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/results/ShipRate.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/results/TestResultSetMapping.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/MappedQueryResultObjectProvider.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/MappedQueryResultObjectProvider.java index 346e4e474..a57b9fe71 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/MappedQueryResultObjectProvider.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/MappedQueryResultObjectProvider.java @@ -35,6 +35,7 @@ import java.util.Locale; import java.util.Map; import java.util.Stack; +import org.apache.openjpa.jdbc.identifier.DBIdentifier; import org.apache.openjpa.jdbc.meta.ClassMapping; import org.apache.openjpa.jdbc.meta.FieldMapping; import org.apache.openjpa.jdbc.meta.JavaSQLTypes; @@ -407,8 +408,16 @@ class MappedQueryResultObjectProvider protected Object getObjectInternal(Object obj, int metaTypeCode, Object arg, Joins joins) throws SQLException { - if (obj instanceof Column) - return _res.getObject((Column) obj, arg, joins); + if (obj instanceof Column){ + Column col = (Column) obj; + Object resultCol = _pc.getMapping(col.toString()); + if (resultCol != null) { + int javaType = col.getJavaType(); + col = new Column(DBIdentifier.newColumn(resultCol.toString()), col.getTable()); + col.setJavaType(javaType); + } + return _res.getObject(col, arg, joins); + } return _res.getObject(obj, metaTypeCode, arg); } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ResultSetResult.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ResultSetResult.java index 69974ecaf..88abc3550 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ResultSetResult.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ResultSetResult.java @@ -508,6 +508,7 @@ public class ResultSetResult DBIdentifier sName = DBIdentifier.newColumn(obj.toString()); return getResultSet().findColumn(_dict.convertSchemaCase(sName)); } catch (SQLException se) { + _dict.log.trace(se.getMessage()); return 0; } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/results/ShipRate.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/results/ShipRate.java new file mode 100644 index 000000000..a6aa4e70a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/results/ShipRate.java @@ -0,0 +1,47 @@ +/* + * 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.criteria.results; + +import java.math.BigDecimal; + +import javax.persistence.*; + +@Entity +@SqlResultSetMapping(name="selectShipRateMapping", + entities=@EntityResult(entityClass=org.apache.openjpa.persistence.criteria.results.ShipRate.class, + fields = {@FieldResult(name="shipRateId", column = "id"), + @FieldResult(name="billedAsWeight", column = "RBLWGT")}) ) +//Try to create a result set with different column name +//than the attribute name defined in the result entity +@NamedNativeQuery(name = "selectShipRateQuery", +query = "SELECT shipRateId as id, billedAsWeight as RBLWGT from ShipRate", +resultSetMapping="selectShipRateMapping") +public class ShipRate { + @Id + long shipRateId; + + public ShipRate(long shipRateId, BigDecimal billedAsWeight) { + super(); + this.shipRateId = shipRateId; + this.billedAsWeight = billedAsWeight; + } + + private BigDecimal billedAsWeight; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/results/TestResultSetMapping.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/results/TestResultSetMapping.java new file mode 100644 index 000000000..7efad5eaa --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/results/TestResultSetMapping.java @@ -0,0 +1,66 @@ +/* + * 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.results; + +import java.math.BigDecimal; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import org.apache.openjpa.persistence.criteria.results.ShipRate; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +public class TestResultSetMapping extends SQLListenerTestCase{ + + public void setUp() { + setUp(CLEAR_TABLES, ShipRate.class); + } + + public void testQuery () { + EntityManager em = emf.createEntityManager(); + try + { + EntityTransaction tx = em.getTransaction(); + tx.begin(); + ShipRate rate = new ShipRate(1000, new BigDecimal(20.5)); + em.persist(rate); + tx.commit(); + em.close(); + + em = emf.createEntityManager(); + //Query query = em.createNativeQuery(sqlStatement, "ShipRateMapping"); + Query query = em.createNamedQuery("selectShipRateQuery"); + + List results = (List)query.getResultList(); + assertEquals(1, results.size()); + } + catch(RuntimeException x) + { + x.printStackTrace(); + throw x; + } + finally + { + em.close(); + } + } +}