001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.jdbc.annotations;
020:
021: import javax.persistence.EntityManager;
022: import javax.persistence.MapKey;
023:
024: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
025: import org.apache.openjpa.jdbc.meta.ClassMapping;
026: import org.apache.openjpa.jdbc.meta.FieldMapping;
027: import org.apache.openjpa.jdbc.meta.strats.RelationMapInverseKeyFieldStrategy;
028: import org.apache.openjpa.jdbc.meta.strats.RelationMapTableFieldStrategy;
029: import org.apache.openjpa.persistence.OpenJPAEntityManager;
030: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
031:
032: /**
033: * <p>Test the use of the {@link MapKey} annotation. Note that we have
034: * more thorough Kodo core tests for the mappings themselves. See
035: * {@link kodo.jdbc.meta.TestMappedByKeyMaps}.
036: *
037: * @author Abe White
038: */
039: public class TestMapKey extends SingleEMFTestCase {
040:
041: public void setUp() {
042: setUp(Flat1.class, AnnoTest1.class, AnnoTest2.class,
043: CLEAR_TABLES);
044:
045: }
046:
047: public void testDefaultMapKeyMapping() {
048: ClassMapping mapping = ((JDBCConfiguration) emf
049: .getConfiguration()).getMappingRepositoryInstance()
050: .getMapping(AnnoTest1.class, null, true);
051: FieldMapping fm = mapping.getFieldMapping("defaultMapKey");
052: assertTrue(fm.getStrategy() instanceof RelationMapTableFieldStrategy);
053: assertEquals("pk", fm.getKey().getValueMappedBy());
054: assertEquals(mapping.getRepository().getMetaData(Flat1.class,
055: null, true).getField("pk"), fm.getKey()
056: .getValueMappedByMetaData());
057: }
058:
059: public void testNamedMapKeyMapping() {
060: ClassMapping mapping = ((JDBCConfiguration) emf
061: .getConfiguration()).getMappingRepositoryInstance()
062: .getMapping(AnnoTest1.class, null, true);
063: FieldMapping fm = mapping.getFieldMapping("namedMapKey");
064: assertTrue(fm.getStrategy() instanceof RelationMapTableFieldStrategy);
065: assertEquals("basic", fm.getKey().getValueMappedBy());
066: assertEquals(mapping.getRepository().getMetaData(Flat1.class,
067: null, true).getField("basic"), fm.getKey()
068: .getValueMappedByMetaData());
069: }
070:
071: public void testInverseOwnerMapKeyMapping() {
072: ClassMapping mapping = ((JDBCConfiguration) emf
073: .getConfiguration()).getMappingRepositoryInstance()
074: .getMapping(AnnoTest1.class, null, true);
075: FieldMapping fm = mapping.getFieldMapping("inverseOwnerMapKey");
076: assertTrue(fm.getStrategy() instanceof RelationMapInverseKeyFieldStrategy);
077: assertEquals("basic", fm.getKey().getValueMappedBy());
078: assertEquals(mapping.getRepository().getMetaData(
079: AnnoTest2.class, null, true).getField("basic"), fm
080: .getKey().getValueMappedByMetaData());
081: }
082:
083: public void testInsertAndRetrieve() {
084: Flat1 f1 = new Flat1(1);
085: f1.setBasic(100);
086: Flat1 f2 = new Flat1(2);
087: f2.setBasic(200);
088: AnnoTest2 a1 = new AnnoTest2(1L, "1");
089: a1.setBasic("100");
090: AnnoTest2 a2 = new AnnoTest2(2L, "2");
091: a2.setBasic("200");
092:
093: AnnoTest1 pc = new AnnoTest1(1L);
094: pc.getDefaultMapKey().put(f1.getPk(), f1);
095: pc.getDefaultMapKey().put(f2.getPk(), f2);
096: pc.getNamedMapKey().put(f1.getBasic(), f1);
097: pc.getNamedMapKey().put(f2.getBasic(), f2);
098: pc.getInverseOwnerMapKey().put(a1.getBasic(), a1);
099: pc.getInverseOwnerMapKey().put(a2.getBasic(), a2);
100: a1.setOneManyOwner(pc);
101: a2.setOneManyOwner(pc);
102:
103: OpenJPAEntityManager em = emf.createEntityManager();
104: em.getTransaction().begin();
105: em.persistAll(new Object[] { pc, f1, f2, a1, a2 });
106: em.getTransaction().commit();
107: em.close();
108:
109: em = emf.createEntityManager();
110: pc = em.find(AnnoTest1.class, new Long(1));
111: assertEquals(2, pc.getDefaultMapKey().size());
112: assertEquals(1, pc.getDefaultMapKey().get(1).getPk());
113: assertEquals(2, pc.getDefaultMapKey().get(2).getPk());
114: assertEquals(2, pc.getNamedMapKey().size());
115: assertEquals(100, pc.getNamedMapKey().get(100).getBasic());
116: assertEquals(200, pc.getNamedMapKey().get(200).getBasic());
117: assertEquals(2, pc.getInverseOwnerMapKey().size());
118: assertEquals("100", pc.getInverseOwnerMapKey().get("100")
119: .getBasic());
120: assertEquals("200", pc.getInverseOwnerMapKey().get("200")
121: .getBasic());
122: em.close();
123: }
124: }
|