Author: ppoddar
Date: Thu May 7 17:36:05 2009
New Revision: 772721
URL: http://svn.apache.org/viewvc?rev=772721&view=rev
Log:
Demonstrates that related instances can be stored across slices (i.e.
collocation constrain can be bypassed) and the across-slice relations can be
fetched reliably under certain restrictions (e.g. for lazy relations).
Added:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Car.java
(with props)
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Manufacturer.java
(with props)
Modified:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/policy/EvenOddDistributionPolicy.java
openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml
Added:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Car.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Car.java?rev=772721&view=auto
==============================================================================
--- openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Car.java
(added)
+++ openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Car.java
Thu May 7 17:36:05 2009
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.slice;
+
+import javax.persistence.*;
+
+...@entity
+public class Car {
+ private String vin;
+
+ private String model;
+
+ private Manufacturer maker;
+
+ @Id
+ public String getVin() {
+ return vin;
+ }
+
+ public void setVin(String vin) {
+ this.vin = vin;
+ }
+
+ @Basic
+ public String getModel() {
+ return model;
+ }
+
+ public void setModel(String model) {
+ this.model = model;
+ }
+
+ @ManyToOne(fetch=FetchType.LAZY)
+ public Manufacturer getMaker() {
+ return maker;
+ }
+
+ public void setMaker(Manufacturer maker) {
+ this.maker = maker;
+ }
+}
Propchange:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Car.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Manufacturer.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Manufacturer.java?rev=772721&view=auto
==============================================================================
---
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Manufacturer.java
(added)
+++
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Manufacturer.java
Thu May 7 17:36:05 2009
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.slice;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+...@entity
+public class Manufacturer {
+ private String name;
+
+ @Id
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Propchange:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/Manufacturer.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java?rev=772721&r1=772720&r2=772721&view=diff
==============================================================================
---
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
(original)
+++
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/TestQuery.java
Thu May 7 17:36:05 2009
@@ -41,7 +41,8 @@
}
public void setUp() throws Exception {
- super.setUp(PObject.class, Person.class, Address.class, Country.class,
+ super.setUp(PObject.class, Person.class, Address.class, Country.class,
+ Car.class, Manufacturer.class,
CLEAR_TABLES);
int count = count(PObject.class);
if (count == 0) {
@@ -225,6 +226,59 @@
em.getTransaction().rollback();
}
+ /**
+ * Verifies that a lazy relation can be stored across different slices i.e.
+ * collocation constraint can be violated under some restrictions.
+ *
+ * Car refers to Manufacturer. The relationship is uni-directional, no
+ * cascade and most importantly lazy.
+ * The distribution policy is designed to store Car and Manufacturer
+ * *always* in different slices.
+ *
+ */
+ public void testCollocationConstraintViolation() {
+ // This transaction will store Manufacturer only
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ Manufacturer bmw = new Manufacturer();
+ bmw.setName("BMW");
+ em.persist(bmw);
+ em.getTransaction().commit();
+
+ // This transaction will store a Car in a slice but the Car is related
+ // to a Manufacturer that *always* reside in a different slice.
+ em.getTransaction().begin();
+ Car z4 = new Car();
+ z4.setVin("1234V56789");
+ z4.setMaker(bmw);
+ z4.setModel("Z4");
+ em.persist(z4);
+ em.getTransaction().commit();
+ em.clear();
+
+ // Verify that all cars are stored in "Even" slice
+ List cars = em.createQuery("select c from Car c").getResultList();
+ assertFalse(cars.isEmpty());
+ for (Object c : cars)
+ assertEquals("Even", SlicePersistence.getSlice(c));
+
+ // While all Manufacturers are stored in "Odd" slice.
+ List makers = em.createQuery("select m from Manufacturer
m").getResultList();
+ assertFalse(makers.isEmpty());
+ for (Object m : makers)
+ assertEquals("Odd", SlicePersistence.getSlice(m));
+ em.clear();
+
+ // Now query for cars. The related manufacturer will be fetched
+ // correctly, though it resides in a different slice because the
+ // relationship is lazy and hence two separate SQLs are issued for
+ // Car and Manufacturer rather than a single SQL with JOIN.
+ cars = em.createQuery("select c from Car c").getResultList();
+ assertFalse(cars.isEmpty());
+ for (Object c : cars)
+ assertNotNull(((Car)c).getMaker());
+ }
+
void assertValidResult(List result) {
assertNotNull(result);
assertFalse(result.isEmpty());
Modified:
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/policy/EvenOddDistributionPolicy.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/policy/EvenOddDistributionPolicy.java?rev=772721&r1=772720&r2=772721&view=diff
==============================================================================
---
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/policy/EvenOddDistributionPolicy.java
(original)
+++
openjpa/trunk/openjpa-slice/src/test/java/org/apache/openjpa/slice/policy/EvenOddDistributionPolicy.java
Thu May 7 17:36:05 2009
@@ -20,7 +20,9 @@
import java.util.List;
+import org.apache.openjpa.slice.Car;
import org.apache.openjpa.slice.DistributionPolicy;
+import org.apache.openjpa.slice.Manufacturer;
import org.apache.openjpa.slice.*;
@@ -35,7 +37,20 @@
char firstChar = Character.toLowerCase(name.charAt(0));
return (firstChar >= 'a' && firstChar <='m') ? "Even" : "Odd";
}
+ if (pc instanceof Car)
+ return distribute((Car)pc);
+ if (pc instanceof Manufacturer)
+ return distribute((Manufacturer)pc);
+
return null;
}
+
+ String distribute(Car car) {
+ return "Even";
+ }
+
+ String distribute(Manufacturer maker) {
+ return "Odd";
+ }
}
Modified:
openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml?rev=772721&r1=772720&r2=772721&view=diff
==============================================================================
--- openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml
(original)
+++ openjpa/trunk/openjpa-slice/src/test/resources/META-INF/persistence.xml Thu
May 7 17:36:05 2009
@@ -105,6 +105,8 @@
<class>org.apache.openjpa.slice.Person</class>
<class>org.apache.openjpa.slice.Address</class>
<class>org.apache.openjpa.slice.Country</class>
+ <class>org.apache.openjpa.slice.Car</class>
+ <class>org.apache.openjpa.slice.Manufacturer</class>
<properties>
<property name="openjpa.BrokerFactory" value="slice"/>
<property name="openjpa.ConnectionDriverName"
value="org.apache.derby.jdbc.EmbeddedDriver"/>