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
This commit is contained in:
Heath Thomann 2012-05-31 13:39:52 +00:00
parent 11cd0d5fd4
commit ebe06fdf35
4 changed files with 125 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@ -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<ShipRate> results = (List<ShipRate>)query.getResultList();
assertEquals(1, results.size());
}
catch(RuntimeException x)
{
x.printStackTrace();
throw x;
}
finally
{
em.close();
}
}
}