mirror of https://github.com/apache/openjpa.git
OPENJPA-1149: Add embeddables sample to the examples module. Patch contributed by Rick Curtis.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@815501 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8fc1404dad
commit
eb5bccdd49
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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>
|
Loading…
Reference in New Issue