Fixes to parse and use SqlResultSetMapping, SqlResultSetMappings annotations.

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@498850 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Srinivasa Segu 2007-01-22 23:33:08 +00:00
parent d88af9faaf
commit 45d90fd282
11 changed files with 246 additions and 6 deletions

View File

@ -199,8 +199,11 @@ public class MappingRepository
if ((getSourceMode() & MODE_QUERY) == 0)
return null;
if (cls == null)
cls = getMetaDataFactory()
.getResultSetMappingScope(name, envLoader);
// not in cache; load
getMetaDataFactory().load(cls, MODE_QUERY, envLoader);
getMetaDataFactory().load(cls, MODE_META | MODE_MAPPING, envLoader);
return (QueryResultMapping) _results.get(key);
}

View File

@ -409,9 +409,12 @@ public class QueryResultMapping
if (last == null)
throw new MetaDataException(_loc.get("untraversable-path",
QueryResultMapping.this, _candidate, path));
assertSingleColumn(last.getColumns(), path);
Column col = last.getColumns()[0];
Column[] cols = last.getColumns();
if (last.isVersion())
cols = candidate.getVersion().getColumns();
assertSingleColumn(cols, path);
Column col = cols[0];
// special-case oid fields, since path lists supplied for
// them at runtime don't include the embedded fields
if (fm != null && fm.getDeclaredTypeCode() == JavaTypes.OID) {

View File

@ -97,6 +97,11 @@ public abstract class AbstractMetaDataFactory
return null;
}
public Class getResultSetMappingScope(String resultSetMappingName,
ClassLoader loader) {
return null;
}
public ClassArgParser newClassArgParser() {
return new ClassArgParser();
}

View File

@ -103,6 +103,11 @@ public class DelegatingMetaDataFactory
return _delegate.getQueryScope(queryName, loader);
}
public Class getResultSetMappingScope(String resultSetMappingName,
ClassLoader loader) {
return _delegate.getResultSetMappingScope(resultSetMappingName, loader);
}
public void clear() {
_delegate.clear();
}
@ -113,5 +118,5 @@ public class DelegatingMetaDataFactory
public void addFieldExtensionKeys(Collection exts) {
_delegate.addFieldExtensionKeys(exts);
}
}
}

View File

@ -115,6 +115,12 @@ public interface MetaDataFactory
*/
public Class getQueryScope(String queryName, ClassLoader loader);
/**
* Return the type defining the given result set mapping name, if any.
*/
public Class getResultSetMappingScope(String resultSetMappingName,
ClassLoader loader);
/**
* Return a properly-configured class arg parser for our expected
* metadata format.

View File

@ -76,6 +76,11 @@ public class NoneMetaDataFactory
return null;
}
public Class getResultSetMappingScope(String resultSetMappingName,
ClassLoader loader) {
return null;
}
public ClassArgParser newClassArgParser() {
return new ClassArgParser();
}

View File

@ -52,7 +52,7 @@ public class QueryMetaData
private String[] _comments;
private List _hintKeys;
private List _hintVals;
private String _resultSetMappingName;
/**
* Construct with the given name.
*/
@ -178,6 +178,14 @@ public class QueryMetaData
_hintVals.add(value);
}
public String getResultSetMappingName() {
return _resultSetMappingName;
}
public void setResultSetMappingName(String setMappingName) {
_resultSetMappingName = setMappingName;
}
/**
* Set query template information into the given concrete
* query instance. However, the language, query string, and
@ -193,6 +201,8 @@ public class QueryMetaData
query.setResultType(_res);
if (_readOnly != null)
query.setReadOnly(_readOnly.booleanValue());
if (_resultSetMappingName != null)
query.setResultMapping(null, _resultSetMappingName);
}
/**

View File

@ -0,0 +1,90 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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 javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
@NamedNativeQueries( {
@NamedNativeQuery(name = "findSimpleEntitites",
query = "SELECT ID, NAME, VALUE FROM SIMPLE_ENTITY",
resultSetMapping = "simpleEntitiesResult") })
@SqlResultSetMapping(name = "simpleEntitiesResult",
entities = @EntityResult(
entityClass = org.apache.openjpa.persistence.query.SimpleEntity.class,
fields = {@FieldResult(name = "id", column = "ID"),
@FieldResult(name = "name", column = "NAME"),
@FieldResult(name = "value", column = "VALUE") }))
@Entity(name = "simple")
@Table(name = "SIMPLE_ENTITY")
public class SimpleEntity {
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@Basic
@Column(name = "NAME")
private String name;
@Basic
@Column(name = "VALUE")
private String value;
public SimpleEntity() {
}
public SimpleEntity(String name, String value) {
this();
this.name = name;
this.value = value;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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 java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
public class TestResultSetMapping extends TestCase {
private EntityManagerFactory emf;
public void setUp() {
Map props = new HashMap();
props.put("openjpa.MetaDataFactory", "jpa(Types=" +
org.apache.openjpa.persistence.query.SimpleEntity.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new SimpleEntity("tName", "tValue"));
em.getTransaction().commit();
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from simple").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testSimpleQuery() {
EntityManager em = emf.createEntityManager();
Query q = em.createNamedQuery("findSimpleEntitites");
List res = q.getResultList();
assertNotNull(res);
for (Iterator resultIter = res.iterator(); resultIter.hasNext();) {
assertSame(resultIter.next().getClass(), SimpleEntity.class);
}
em.close();
}
public static void main(String[] args) {
TestRunner.run(TestResultSetMapping.class);
}
}

View File

@ -1555,6 +1555,9 @@ public class AnnotationPersistenceMetaDataParser
else if (!void.class.equals(res))
meta.setResultType(res);
if (!StringUtils.isEmpty(query.resultSetMapping()))
meta.setResultSetMappingName(query.resultSetMapping());
meta.setSource(getSourceFile(), (el instanceof Class) ? el : null,
meta.SRC_ANNOTATIONS);
if (isMetaDataMode())

View File

@ -32,6 +32,8 @@ import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
import org.apache.openjpa.lib.conf.Configurable;
import org.apache.openjpa.lib.conf.Configuration;
@ -294,6 +296,28 @@ public class PersistenceMetaDataFactory
return null;
}
@Override
public Class getResultSetMappingScope(String rsMappingName,
ClassLoader loader) {
if (rsMappingName == null)
return null;
Collection classes = repos.loadPersistentTypes(false, loader);
for (Class cls : (Collection<Class>) classes) {
if (cls.isAnnotationPresent(SqlResultSetMapping.class) &&
hasRSMapping(rsMappingName, (SqlResultSetMapping) cls.
getAnnotation(SqlResultSetMapping.class)))
return cls;
if (cls.isAnnotationPresent(SqlResultSetMappings.class) &&
hasRSMapping(rsMappingName, ((SqlResultSetMappings) cls.
getAnnotation(SqlResultSetMappings.class)).value()))
return cls;
}
return null;
}
private boolean hasNamedQuery(String query, NamedQuery... queries) {
for (NamedQuery q : queries) {
if (query.equals(q.name()))
@ -302,6 +326,15 @@ public class PersistenceMetaDataFactory
return false;
}
private boolean hasRSMapping(String rsMapping,
SqlResultSetMapping... mappings) {
for (SqlResultSetMapping m : mappings) {
if (rsMapping.equals(m.name()))
return true;
}
return false;
}
private boolean hasNamedNativeQuery(String query,
NamedNativeQuery... queries) {
for (NamedNativeQuery q : queries) {