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:
023: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
024:
025: /**
026: * Test for 1-1
027: *
028: * @author Steve Kim
029: */
030: public class TestOneToOne extends SingleEMFTestCase {
031:
032: public void setUp() {
033: setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class,
034: CLEAR_TABLES);
035: }
036:
037: public void testOneToOne() {
038: EntityManager em = emf.createEntityManager();
039: em.getTransaction().begin();
040: AnnoTest1 pc = new AnnoTest1(5);
041: AnnoTest2 pc2 = new AnnoTest2(15, "foo");
042: pc.setOneOne(pc2);
043: em.persist(pc);
044: em.persist(pc2);
045: em.getTransaction().commit();
046: em.close();
047:
048: em = emf.createEntityManager();
049: pc = em.find(AnnoTest1.class, new Long(5));
050: pc2 = pc.getOneOne();
051: assertNotNull(pc2);
052: assertEquals(15, pc2.getPk1());
053: assertEquals("foo", pc2.getPk2());
054: em.close();
055: }
056:
057: public void testSelfOneToOne() {
058: EntityManager em = emf.createEntityManager();
059: em.getTransaction().begin();
060: AnnoTest1 pc = new AnnoTest1(5);
061: AnnoTest1 pc2 = new AnnoTest1(15);
062: pc.setSelfOneOne(pc2);
063: em.persist(pc);
064: em.persist(pc2);
065: em.getTransaction().commit();
066: em.close();
067:
068: em = emf.createEntityManager();
069: pc = em.find(AnnoTest1.class, new Long(5));
070: pc2 = pc.getSelfOneOne();
071: assertNotNull(pc2);
072: assertEquals(new Long(15), pc2.getPk());
073: em.close();
074: }
075:
076: public void testPKJoinSelfOneToOne() {
077: EntityManager em = emf.createEntityManager();
078: em.getTransaction().begin();
079: AnnoTest1 pc = new AnnoTest1(5);
080: AnnoTest1 pc2 = new AnnoTest1(15);
081: pc.setSelfOneOne(pc2);
082: em.persist(pc);
083: em.persist(pc2);
084: em.getTransaction().commit();
085: em.close();
086:
087: em = emf.createEntityManager();
088: pc = em.find(AnnoTest1.class, new Long(5));
089: pc2 = pc.getSelfOneOne();
090: assertNotNull(pc2);
091: assertEquals(new Long(15), pc2.getPk());
092: em.close();
093: }
094:
095: public void testOtherTableOneToOne() {
096: EntityManager em = emf.createEntityManager();
097: em.getTransaction().begin();
098: AnnoTest1 pc = new AnnoTest1(5);
099: AnnoTest2 pc2 = new AnnoTest2(15, "foo");
100: pc.setOtherTableOneOne(pc2);
101: em.persist(pc);
102: em.persist(pc2);
103: em.getTransaction().commit();
104: em.close();
105:
106: em = emf.createEntityManager();
107: pc = em.find(AnnoTest1.class, new Long(5));
108: pc2 = pc.getOtherTableOneOne();
109: assertNotNull(pc2);
110: assertEquals(15, pc2.getPk1());
111: assertEquals("foo", pc2.getPk2());
112: em.close();
113: }
114:
115: public void testInverseOneToOne() {
116: EntityManager em = emf.createEntityManager();
117: em.getTransaction().begin();
118: AnnoTest1 pc = new AnnoTest1(5);
119: AnnoTest2 pc2 = new AnnoTest2(15, "foo");
120: pc2.setInverseOneOne(pc);
121: em.persist(pc);
122: em.persist(pc2);
123: em.getTransaction().commit();
124: em.close();
125:
126: em = emf.createEntityManager();
127: pc = em.find(AnnoTest1.class, new Long(5));
128: pc2 = pc.getInverseOwnerOneOne();
129: assertNotNull(pc2);
130: assertEquals(15, pc2.getPk1());
131: assertEquals("foo", pc2.getPk2());
132: assertEquals(pc, pc2.getInverseOneOne());
133: em.close();
134: }
135: }
|