mirror of https://github.com/apache/openjpa.git
OPENJPA-1232:
Call toNestedFields for Map Keys as well as Map values. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@802211 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a54f47dfc8
commit
3b327b5fbb
|
@ -95,8 +95,8 @@ public abstract class AbstractPCData
|
||||||
keys.add(e.getKey());
|
keys.add(e.getKey());
|
||||||
values.add(e.getValue());
|
values.add(e.getValue());
|
||||||
}
|
}
|
||||||
|
Object[] keyArray = toNestedFields(sm, fmd.getKey(),
|
||||||
Object[] keyArray = keys.toArray();
|
keys, fetch, context).toArray();
|
||||||
Object[] valueArray = toNestedFields(sm, fmd.getElement(),
|
Object[] valueArray = toNestedFields(sm, fmd.getElement(),
|
||||||
values, fetch, context).toArray();
|
values, fetch, context).toArray();
|
||||||
for (int idx = 0; idx < keyArray.length; idx++)
|
for (int idx = 0; idx < keyArray.length; idx++)
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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.datacache;
|
||||||
|
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
|
|
||||||
|
@Embeddable
|
||||||
|
public class MapEmbeddable {
|
||||||
|
private int value;
|
||||||
|
|
||||||
|
MapEmbeddable(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MapEmbeddable [_value=" + value + "]";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
/*
|
||||||
|
* 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.datacache;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Version;
|
||||||
|
|
||||||
|
@Entity(name = "MapHolder")
|
||||||
|
public class MapHolder {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@Version
|
||||||
|
private int version;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
private Map<MapEmbeddable, MapEmbeddable> embeddableMap;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(int version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<MapEmbeddable, MapEmbeddable> getEmbeddableMap() {
|
||||||
|
return embeddableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmbeddableMap(Map<MapEmbeddable, MapEmbeddable> embeddableMap) {
|
||||||
|
this.embeddableMap = embeddableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MapHolder [embeddableMap=" + embeddableMap + ", id=" + id + ", version=" + version + "]";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
/*
|
||||||
|
* 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.datacache;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
|
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
|
||||||
|
import org.apache.openjpa.persistence.test.PersistenceTestCase;
|
||||||
|
|
||||||
|
public class TestEntitiesAsKeys extends PersistenceTestCase {
|
||||||
|
|
||||||
|
OpenJPAEntityManagerFactorySPI emf =
|
||||||
|
createEMF(MapHolder.class, MapEmbeddable.class, "openjpa.DataCache", "true", "openjpa.RemoteCommitProvider",
|
||||||
|
"sjvm", "openjpa.RuntimeUnenhancedClasses", "unsupported");
|
||||||
|
|
||||||
|
public void setUp() {
|
||||||
|
populate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void populate() {
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
// clean up before execution
|
||||||
|
em.getTransaction().begin();
|
||||||
|
em.createQuery("Delete from MapHolder").executeUpdate();
|
||||||
|
em.getTransaction().commit();
|
||||||
|
|
||||||
|
em.getTransaction().begin();
|
||||||
|
MapHolder mh = new MapHolder();
|
||||||
|
mh.setId(10);
|
||||||
|
mh.setEmbeddableMap(getEmbeddableMap(1, 2, 3, 4, 5, 6, 7, 8));
|
||||||
|
em.persist(mh);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testMapContents() {
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
|
MapHolder mh = em.find(MapHolder.class, 10);
|
||||||
|
mh.getEmbeddableMap();
|
||||||
|
assertNotNull(mh);
|
||||||
|
|
||||||
|
for (Object o : mh.getEmbeddableMap().keySet()) {
|
||||||
|
assertTrue("Expected key to be instanceof MapEmbeddable but was " + o.getClass().getCanonicalName(),
|
||||||
|
o instanceof MapEmbeddable);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Object o : mh.getEmbeddableMap().values()) {
|
||||||
|
assertTrue("Expected value to be instanceof MapEmbeddable but was " + o.getClass().getCanonicalName(),
|
||||||
|
o instanceof MapEmbeddable);
|
||||||
|
}
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<MapEmbeddable, MapEmbeddable> getEmbeddableMap(Integer... integers) {
|
||||||
|
Map<MapEmbeddable, MapEmbeddable> rval = new HashMap<MapEmbeddable, MapEmbeddable>();
|
||||||
|
assertEquals(0, integers.length % 2);
|
||||||
|
|
||||||
|
for (int i = 0; i < integers.length; i += 2) {
|
||||||
|
rval.put(new MapEmbeddable(integers[i]), new MapEmbeddable(integers[i + 1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue