01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.persistence.jdbc.annotations;
20:
21: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
22: import org.apache.openjpa.jdbc.meta.ClassMapping;
23: import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy;
24: import org.apache.openjpa.jdbc.meta.strats.NoneDiscriminatorStrategy;
25: import org.apache.openjpa.persistence.OpenJPAEntityManager;
26: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
27:
28: /**
29: * <p>Test that InheritanceType.TABLE_PER_CLASS JPA mapping is translated
30: * correctly. See the <code>kodo.jdbc.meta.tableperclass</code> test package
31: * for more detailed tests of functionality.</p>
32: *
33: * @author Abe White
34: */
35: public class TestTablePerClassInheritance extends SingleEMFTestCase {
36:
37: public void setUp() {
38: setUp(TablePerClass1.class, TablePerClass2.class,
39: EmbedOwner.class, EmbedValue.class, CLEAR_TABLES);
40: }
41:
42: public void testMapping() {
43: ClassMapping mapping = ((JDBCConfiguration) emf
44: .getConfiguration()).getMappingRepositoryInstance()
45: .getMapping(TablePerClass2.class, null, true);
46: assertTrue(mapping.getStrategy() instanceof FullClassStrategy);
47: assertTrue(mapping.getDiscriminator().getStrategy() instanceof NoneDiscriminatorStrategy);
48: assertNull(mapping.getJoinForeignKey());
49: assertNull(mapping.getJoinablePCSuperclassMapping());
50: assertEquals("TPC_BASIC", mapping.getFieldMapping("basic")
51: .getColumns()[0].getName());
52: ClassMapping embed = mapping.getFieldMapping("embed")
53: .getEmbeddedMapping();
54: assertEquals("TPC_EMB_BASIC", embed.getFieldMapping("basic")
55: .getColumns()[0].getName());
56:
57: ClassMapping sup = mapping.getPCSuperclassMapping();
58: assertEquals(TablePerClass1.class, sup.getDescribedType());
59: assertTrue(sup.getStrategy() instanceof FullClassStrategy);
60: assertTrue(sup.getDiscriminator().getStrategy() instanceof NoneDiscriminatorStrategy);
61: assertEquals("TPC_BASIC", sup.getFieldMapping("basic")
62: .getColumns()[0].getName());
63: embed = sup.getFieldMapping("embed").getEmbeddedMapping();
64: assertEquals("TPC_EMB_BASIC", embed.getFieldMapping("basic")
65: .getColumns()[0].getName());
66: }
67:
68: public void testInsertAndRetrieve() {
69: OpenJPAEntityManager em = emf.createEntityManager();
70: em.getTransaction().begin();
71: TablePerClass1 tpc1 = new TablePerClass1();
72: tpc1.setBasic(1);
73: EmbedValue ev = new EmbedValue();
74: ev.setBasic("11");
75: tpc1.setEmbed(ev);
76: TablePerClass2 tpc2 = new TablePerClass2();
77: tpc2.setBasic(2);
78: tpc2.setBasic2("2");
79: ev = new EmbedValue();
80: ev.setBasic("22");
81: tpc2.setEmbed(ev);
82: em.persistAll(tpc1, tpc2);
83: em.getTransaction().commit();
84: int id1 = tpc1.getPk();
85: int id2 = tpc2.getPk();
86: em.close();
87:
88: em = emf.createEntityManager();
89: tpc1 = em.find(TablePerClass1.class, id1);
90: assertEquals(1, tpc1.getBasic());
91: assertEquals("11", tpc1.getEmbed().getBasic());
92: tpc2 = (TablePerClass2) em.find(TablePerClass1.class, id2);
93: assertEquals(2, tpc2.getBasic());
94: assertEquals("2", tpc2.getBasic2());
95: assertEquals("22", tpc2.getEmbed().getBasic());
96: em.close();
97: }
98: }
|