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.annotations;
020:
021: import javax.persistence.*;
022:
023: import org.apache.openjpa.enhance.*;
024: import org.apache.openjpa.persistence.OpenJPAEntityManager;
025:
026: import junit.framework.*;
027:
028: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
029: import org.apache.openjpa.persistence.common.utils.*;
030:
031: /**
032: * <p>Test embedded id classes.</p>
033: *
034: * @author Abe White
035: */
036: public class TestEmbeddedId extends AnnotationTestCase {
037:
038: public TestEmbeddedId(String name) {
039: super (name, "annotationcactusapp");
040: }
041:
042: EmbeddedIdClass _oid;
043: EmbeddedIdClass _roid;
044:
045: public void setUp() {
046: deleteAll(EmbeddedIdEntity.class);
047:
048: _oid = new EmbeddedIdClass();
049: _oid.setPk1(1);
050: _oid.setPk2(2);
051:
052: EmbeddedIdEntity e = new EmbeddedIdEntity();
053: e.setId(_oid);
054: e.setValue("e");
055:
056: _roid = new EmbeddedIdClass();
057: _roid.setPk1(2);
058: _roid.setPk2(3);
059:
060: EmbeddedIdEntity rel = new EmbeddedIdEntity();
061: rel.setId(_roid);
062: rel.setValue("r");
063: e.setRelation(rel);
064:
065: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
066: startTx(em);
067: em.persistAll(e, rel);
068: endTx(em);
069: endEm(em);
070: }
071:
072: public void testGetObjectId() {
073: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
074: EmbeddedIdEntity e = em.find(EmbeddedIdEntity.class, _oid);
075: assertNotNull(e);
076: assertEquals("e", e.getValue());
077: assertNull(e.getMappingOverrideRelation());
078: assertNotNull(e.getRelation());
079: assertEquals("r", e.getRelation().getValue());
080:
081: assertEquals(_oid, em.getObjectId(e));
082: assertEquals(_roid, em.getObjectId(e.getRelation()));
083: assertEquals(_oid, e.getId());
084: assertEquals(_roid, e.getRelation().getId());
085: assertNull(((PersistenceCapable) e.getId())
086: .pcGetGenericContext());
087: endEm(em);
088: }
089:
090: public void testMutateEmbeddedIdFieldValueOfNew() {
091: EmbeddedIdEntity e1 = new EmbeddedIdEntity();
092: e1.setValue("e1");
093: EmbeddedIdEntity e2 = new EmbeddedIdEntity();
094: e2.setValue("e2");
095:
096: e2.setId(new EmbeddedIdClass());
097: //Comment this since pk3 is auto-generated and assigning
098: //it explicitly causes a rollback
099: //e2.getId().setPk3(99); // prevent assign on access
100:
101: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
102: startTx(em);
103: //em.persistAll(e1, e2);
104:
105: EmbeddedIdClass oid = new EmbeddedIdClass();
106: oid.setPk1(4);
107: oid.setPk2(5);
108: //Comment this since pk3 is auto-generated and assigning
109: //it explicitly causes a rollback
110: //oid.setPk3(6);
111: e1.setId(oid);
112: e2.getId().setPk1(6);
113: e2.getId().setPk2(7);
114: em.persistAll(e1, e2);
115:
116: endTx(em);
117:
118: EmbeddedIdClass oid1 = e1.getId();
119: EmbeddedIdClass oid2 = e2.getId();
120: assertEquals(oid1, em.getObjectId(e1));
121: assertEquals(oid2, em.getObjectId(e2));
122: assertEquals(4, oid1.getPk1());
123: assertEquals(5, oid1.getPk2());
124: assertEquals(6, oid2.getPk1());
125: assertEquals(7, oid2.getPk2());
126: endEm(em);
127:
128: em = (OpenJPAEntityManager) currentEntityManager();
129: e1 = em.find(EmbeddedIdEntity.class, oid1);
130: e2 = em.find(EmbeddedIdEntity.class, oid2);
131: assertEquals(oid1, em.getObjectId(e1));
132: assertEquals(oid2, em.getObjectId(e2));
133: assertEquals(oid1, e1.getId());
134: assertEquals(oid2, e2.getId());
135: endEm(em);
136: }
137:
138: public void testMutateEmbeddedIdFieldValueOfExisting() {
139: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
140: startTx(em);
141:
142: EmbeddedIdEntity e = em.find(EmbeddedIdEntity.class, _oid);
143: e.setValue("changed");
144: try {
145: e.getId().setPk1(9);
146: endTx(em);
147: fail("Committed with changed oid field.");
148: } catch (RuntimeException re) {
149: if (isActiveTx(em))
150: rollbackTx(em);
151: } catch (Exception exc) {
152: if (isActiveTx(em))
153: rollbackTx(em);
154: }
155: endEm(em);
156: }
157:
158: public void testDetachAttach() {
159: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
160: EmbeddedIdEntity e = em.find(EmbeddedIdEntity.class, _oid);
161: e.getRelation();
162: endEm(em);
163:
164: e.setValue("echanged");
165: e.getRelation().setValue("rchanged");
166:
167: em = (OpenJPAEntityManager) currentEntityManager();
168: startTx(em);
169: EmbeddedIdEntity me = (EmbeddedIdEntity) em.mergeAll(e, e
170: .getRelation())[0];
171: assertTrue(me != e);
172: assertNotNull(me.getRelation());
173: assertTrue(me.getRelation() != e.getRelation());
174: assertEquals("echanged", me.getValue());
175: assertEquals("rchanged", me.getRelation().getValue());
176: assertEquals(_oid, me.getId());
177: assertEquals(_oid, em.getObjectId(me));
178: assertEquals(_roid, me.getRelation().getId());
179: assertEquals(_roid, em.getObjectId(me.getRelation()));
180: endTx(em);
181: endEm(em);
182: }
183:
184: public void testQuery() {
185: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
186: Query q = em.createQuery("select e from EmbeddedIdEntity e "
187: + "where e.id.pk1 = 1");
188: EmbeddedIdEntity e = (EmbeddedIdEntity) q.getSingleResult();
189: assertEquals(_oid, e.getId());
190: assertEquals("e", e.getValue());
191:
192: q = em.createQuery("select e.id.pk2 from EmbeddedIdEntity e "
193: + "where e.id.pk1 = 1");
194: assertEquals(new Long(_oid.getPk2()), q.getSingleResult());
195:
196: q = em.createQuery("select e.id from EmbeddedIdEntity e "
197: + "where e.id.pk1 = 1");
198: assertEquals(_oid, q.getSingleResult());
199: endEm(em);
200: }
201:
202: public void testAutoAssigned() {
203: // begin with null id object
204: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
205: startTx(em);
206: EmbeddedIdEntity e = new EmbeddedIdEntity();
207: em.persist(e);
208: EmbeddedIdClass oid = e.getId();
209: assertNotNull(oid);
210: assertTrue(oid.getPk3() != 0);
211: assertEquals(oid, em.getObjectId(e));
212: endTx(em);
213: assertEquals(oid, em.getObjectId(e));
214: endEm(em);
215:
216: em = (OpenJPAEntityManager) currentEntityManager();
217: e = em.find(EmbeddedIdEntity.class, oid);
218: assertEquals(oid, em.getObjectId(e));
219: endEm(em);
220:
221: // begin with non-null id object
222: em = (OpenJPAEntityManager) currentEntityManager();
223: startTx(em);
224: e = new EmbeddedIdEntity();
225: oid = new EmbeddedIdClass();
226: oid.setPk1(4);
227: oid.setPk2(5);
228: e.setId(oid);
229: em.persist(e);
230: oid = e.getId();
231: assertEquals(4, oid.getPk1());
232: assertEquals(5, oid.getPk2());
233: assertTrue(oid.getPk3() != 0);
234: assertEquals(oid, em.getObjectId(e));
235: endTx(em);
236: assertEquals(oid, em.getObjectId(e));
237: endEm(em);
238:
239: em = (OpenJPAEntityManager) currentEntityManager();
240: e = em.find(EmbeddedIdEntity.class, oid);
241: assertEquals(oid, em.getObjectId(e));
242: endEm(em);
243:
244: // flush before accessing id field
245: em = (OpenJPAEntityManager) currentEntityManager();
246: startTx(em);
247: e = new EmbeddedIdEntity();
248: em.persist(e);
249: endTx(em);
250: oid = e.getId();
251: assertTrue(oid.getPk3() != 0);
252: assertEquals(oid, em.getObjectId(e));
253: endEm(em);
254:
255: em = (OpenJPAEntityManager) currentEntityManager();
256: e = em.find(EmbeddedIdEntity.class, oid);
257: assertEquals(oid, em.getObjectId(e));
258: endEm(em);
259: }
260: }
|