You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2009/09/15 23:40:59 UTC

svn commit: r815501 - in /openjpa/trunk/openjpa-examples/src/main/java: META-INF/ embeddables/

Author: curtisr7
Date: Tue Sep 15 21:40:59 2009
New Revision: 815501

URL: http://svn.apache.org/viewvc?rev=815501&view=rev
Log:
OPENJPA-1149: Add embeddables sample to the examples module. Patch contributed by Rick Curtis.

Added:
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/Address.java   (with props)
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/ContactInfo.java   (with props)
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/Coordinates.java   (with props)
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/Main.java   (with props)
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/Phone.java   (with props)
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/User.java   (with props)
    openjpa/trunk/openjpa-examples/src/main/java/embeddables/build.xml   (with props)
Modified:
    openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml

Modified: openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml?rev=815501&r1=815500&r2=815501&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml (original)
+++ openjpa/trunk/openjpa-examples/src/main/java/META-INF/persistence.xml Tue Sep 15 21:40:59 2009
@@ -81,4 +81,14 @@
     <persistence-unit name="reversemapping" transaction-type="RESOURCE_LOCAL">
         <mapping-file>reversemapping/orm.xml</mapping-file>
     </persistence-unit>
+    
+    <persistence-unit name="embeddables" transaction-type="RESOURCE_LOCAL">
+        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
+        <class>embeddables.Address</class>
+        <class>embeddables.ContactInfo</class>
+        <class>embeddables.Coordinates</class>
+        <class>embeddables.Phone</class>
+        <class>embeddables.User</class>
+    </persistence-unit>
+    
 </persistence>

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Address.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/Address.java?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/Address.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/Address.java Tue Sep 15 21:40:59 2009
@@ -0,0 +1,56 @@
+/*
+ * 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 embeddables;
+
+import javax.persistence.Basic;
+import javax.persistence.CascadeType;
+import javax.persistence.Embeddable;
+import javax.persistence.ManyToOne;
+
+@Embeddable
+public class Address {
+    @Basic
+    private String street;
+    @Basic
+    private String city;
+    @Basic
+    private String state;
+    @Basic
+    private Integer zip;
+
+    // Relationship from an Embeddable to an Entity
+    @ManyToOne(cascade = CascadeType.ALL)
+    Coordinates coordinates;
+
+    public Address() {
+
+    }
+
+    public Address(String street, String city, String state, Integer zip, Coordinates c) {
+        this.street = street;
+        this.city = city;
+        this.state = state;
+        this.zip = zip;
+        coordinates = c;
+    }
+
+    public String toString() {
+        return street + " " + city + ", " + state + " " + zip;
+    }
+}

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Address.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/ContactInfo.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/ContactInfo.java?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/ContactInfo.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/ContactInfo.java Tue Sep 15 21:40:59 2009
@@ -0,0 +1,45 @@
+/*
+ * 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 embeddables;
+
+import javax.persistence.Embeddable;
+import javax.persistence.Embedded;
+
+@Embeddable
+public class ContactInfo {
+    public ContactInfo() {
+    }
+
+    public ContactInfo(Address a, Phone p) {
+        homeAddress = a;
+        homePhone = p;
+    }
+
+    // Nested embeddable
+    @Embedded
+    Address homeAddress;
+
+    // Nested embeddable
+    @Embedded
+    Phone homePhone;
+
+    public String toString() {
+        return homeAddress.toString() + " " + homePhone.toString();
+    }
+}

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/ContactInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Coordinates.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/Coordinates.java?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/Coordinates.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/Coordinates.java Tue Sep 15 21:40:59 2009
@@ -0,0 +1,45 @@
+/*
+ * 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 embeddables;
+
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+public class Coordinates {
+    @Id
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    int id;
+
+    @Basic
+    String longitude;
+    @Basic
+    String latitude;
+
+    public Coordinates() {
+    }
+
+    public Coordinates(String lon, String lat) {
+        longitude = lon;
+        latitude = lat;
+    }
+}

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Coordinates.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Main.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/Main.java?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/Main.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/Main.java Tue Sep 15 21:40:59 2009
@@ -0,0 +1,97 @@
+/*
+ * 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 embeddables;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import javax.persistence.Query;
+
+public class Main {
+
+    public static void main(String[] args) throws Exception {
+        EntityManagerFactory emf = Persistence.createEntityManagerFactory("embeddables", System.getProperties());
+        EntityManager em = emf.createEntityManager();
+        init(em);
+        runQueries(em);
+    }
+
+    public static void runQueries(EntityManager em) {
+        // Find all users that have a secondary address that is in MI
+        Query q = em.createQuery("SELECT u FROM User u , in (u.addresses) a " + "WHERE a.state='xx'");
+
+        List<User> res = q.getResultList();
+        for (User b : res) {
+            System.out.println(b);
+        }
+
+        Query q1 = em.createQuery("SELECT u FROM User u , in (u.addresses) a " + "WHERE a.coordinates.longitude='38'");
+        List<User> res1 = q1.getResultList();
+        for (User b : res1) {
+            System.out.println(b);
+        }
+
+        // Find users who's conatctInfo cell phone is 507-555-5555
+        // Entity -> embedded -> embedded
+        q =
+            em.createQuery("SELECT DISTINCT u FROM User u " + "WHERE u.contactInfo.homePhone.number='507-555-5555'"
+                + "AND u.contactInfo.homePhone.type='cell'");
+        res = q.getResultList();
+        for (User b : res) {
+            System.out.println(b);
+        }
+
+        // Find users who's conatctInfo cell phone is 507-555-5555
+        // Entity -> embedded -> embedded
+        q =
+            em.createQuery("SELECT u FROM User u " + "WHERE u.contactInfo.homePhone.number='507-555-5555'"
+                + "AND u.contactInfo.homePhone.type='cell'");
+        res = q.getResultList();
+        for (User b : res) {
+            System.out.println(b);
+        }
+
+    }
+
+    public static void init(EntityManager em) {
+        Coordinates c = new Coordinates("37.0", "23.516");
+        Coordinates c1 = new Coordinates("38", "23.516");
+        Coordinates c2 = new Coordinates("39", "23.516");
+
+        Phone p = new Phone("507-555-5555", "cell");
+        Address a = new Address("Cariou Ln", "Minneapolis", "MN", 90210, c);
+        ContactInfo ci = new ContactInfo(a, p);
+
+        User u = new User(ci, "user_name" + System.currentTimeMillis(), "user_asdf");
+        u.addAddress(new Address("100 Rodeo Dr", "Arroyo Grande", "CA", 93420, c1));
+        u.addAddress(new Address("1700 W 3rd Ave", "Flint", "MI", 48504, c2));
+        u.addAddress(new Address("4301 Farm Ln.", "East Lansing", "MI", 48824, c2));
+
+        em.getTransaction().begin();
+        em.persist(c);
+        em.persist(c1);
+        em.persist(c2);
+        em.persist(u);
+        em.getTransaction().commit();
+
+    }
+
+}

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Main.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Phone.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/Phone.java?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/Phone.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/Phone.java Tue Sep 15 21:40:59 2009
@@ -0,0 +1,43 @@
+/*
+ * 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 embeddables;
+
+import javax.persistence.Basic;
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class Phone {
+    public Phone() {
+
+    }
+
+    public Phone(String n, String t) {
+        number = n;
+        type = t;
+    }
+
+    @Basic
+    private String number;
+    @Basic
+    private String type;
+
+    public String toString() {
+        return "type: " + type + " number:" + number;
+    }
+}

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/Phone.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/User.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/User.java?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/User.java (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/User.java Tue Sep 15 21:40:59 2009
@@ -0,0 +1,76 @@
+/*
+ * 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 embeddables;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Basic;
+import javax.persistence.CollectionTable;
+import javax.persistence.ElementCollection;
+import javax.persistence.Embedded;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+// User is a reserved keyword for derby, so use a different table name.
+@Table(name = "User0")
+public class User {
+    @Id
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    private int id;
+    @Embedded
+    ContactInfo contactInfo;
+
+    // Collection of embeddables
+    @ElementCollection
+    @CollectionTable(name = "user_addresses")
+    private Set<Address> addresses = new HashSet<Address>();
+    @Basic
+    private String user_name;
+
+    @Basic
+    private String user;
+
+    public User() {
+    }
+
+    public User(ContactInfo c, String u_n, String u) {
+        contactInfo = c;
+        user = u;
+        user_name = u_n;
+
+    }
+
+    public void addAddress(Address a) {
+        addresses.add(a);
+    }
+
+    public String toString() {
+        String res = user_name + " " + user + " " + contactInfo;
+
+        for (Address a : addresses) {
+            res += "\n" + a.toString();
+        }
+        return res;
+    }
+}

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/User.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-examples/src/main/java/embeddables/build.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/src/main/java/embeddables/build.xml?rev=815501&view=auto
==============================================================================
--- openjpa/trunk/openjpa-examples/src/main/java/embeddables/build.xml (added)
+++ openjpa/trunk/openjpa-examples/src/main/java/embeddables/build.xml Tue Sep 15 21:40:59 2009
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.   
+-->
+<project name="embeddables" default="run">
+    <property name="example" value="embeddables"/>
+    <import file="../build.xml"/>
+</project>

Propchange: openjpa/trunk/openjpa-examples/src/main/java/embeddables/build.xml
------------------------------------------------------------------------------
    svn:eol-style = native