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 java.util.Collection;
022:
023: import org.apache.openjpa.persistence.OpenJPAEntityManager;
024: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
025:
026: /**
027: * Test for 1-m
028: *
029: * @author Steve Kim
030: */
031: public class TestOneToMany extends SingleEMFTestCase {
032:
033: public void setUp() {
034: setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class,
035: CLEAR_TABLES);
036: }
037:
038: public void testOneToMany() {
039: OpenJPAEntityManager em = emf.createEntityManager();
040: em.getTransaction().begin();
041: AnnoTest1 pc = new AnnoTest1(5);
042: pc.getOneMany().add(new AnnoTest2(15, "foo"));
043: pc.getOneMany().add(new AnnoTest2(20, "foobar"));
044: em.persist(pc);
045: em.persistAll(pc.getOneMany());
046: em.getTransaction().commit();
047: em.close();
048:
049: em = emf.createEntityManager();
050: pc = em.find(AnnoTest1.class, new Long(5));
051: Collection<AnnoTest2> many = pc.getOneMany();
052: assertEquals(2, many.size());
053: for (AnnoTest2 pc2 : many) {
054: switch ((int) pc2.getPk1()) {
055: case 15:
056: assertEquals("foo", pc2.getPk2());
057: break;
058: case 20:
059: assertEquals("foobar", pc2.getPk2());
060: break;
061: default:
062: fail("unknown element:" + pc2.getPk1());
063: }
064: }
065: em.close();
066: }
067:
068: public void testInverseOwnerOneToMany() {
069: OpenJPAEntityManager em = emf.createEntityManager();
070: em.getTransaction().begin();
071: AnnoTest1 pc = new AnnoTest1(5);
072: AnnoTest2 pc2 = new AnnoTest2(15, "foo");
073: pc.getInverseOwnerOneMany().add(pc2);
074: pc2.setOneManyOwner(pc);
075: pc2 = new AnnoTest2(20, "foobar");
076: pc.getInverseOwnerOneMany().add(pc2);
077: pc2.setOneManyOwner(pc);
078: em.persist(pc);
079: em.persistAll(pc.getInverseOwnerOneMany());
080: em.getTransaction().commit();
081: em.close();
082:
083: em = emf.createEntityManager();
084: pc = em.find(AnnoTest1.class, new Long(5));
085: Collection<AnnoTest2> many = pc.getInverseOwnerOneMany();
086: assertEquals(2, many.size());
087: for (AnnoTest2 pc3 : many) {
088: assertEquals(pc, pc3.getOneManyOwner());
089: switch ((int) pc3.getPk1()) {
090: case 15:
091: assertEquals("foo", pc3.getPk2());
092: break;
093: case 20:
094: assertEquals("foobar", pc3.getPk2());
095: break;
096: default:
097: fail("unknown element:" + pc3.getPk1());
098: }
099: }
100: em.close();
101: }
102: }
|