Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/PolyglotPersistence"

(New page: This example is a variation of the http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL NoSQL Example with some of the entities mapped to MongoDB and some to relational. The object...)
 
Line 1: Line 1:
This example is a variation of the [[http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL NoSQL Example]with some of the entities mapped to MongoDB and some to relational.  The object model and the configuration for MongoDB is described in that example and is not repeated here.
+
This example is a variation of the [http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL NoSQL Example] with some of the entities mapped to MongoDB and some to relational.  The object model and the configuration for MongoDB is described in that example and is not repeated here.
  
 
==Domain Model==  
 
==Domain Model==  
Line 49: Line 49:
 
     private Product product;
 
     private Product product;
 
...
 
...
 +
</source>
 +
 +
==Composite Persistence Unit==
 +
 +
[http://www.eclipse.org/eclipselink/documentation/2.4/solutions/usingmultipledbs.htm Composite persistence units] provide a way to combine entities associated with different data sources into a single persistence unit.  Whether the data source is relational or non-relation is irrelevant.  Users of the composite persistence unit are unaware that they are working with a composite and that entities returned from queries or through relationship navigation may be coming from different data sources--they simple interact with a set entities.
 +
 +
In this example there are two component persistence units: "relational-pu" containing Discount and Product and "nosql-pu" containing the remaining classes.  The persistence.xml for each of these persistence units lists their classes and associates them with the configured data source:
 +
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
 +
<persistence-unit name="RelationalPU" transaction-type="RESOURCE_LOCAL">
 +
<class>relational.model.Product</class>
 +
<class>relational.model.Discount</class>
 +
<properties>
 +
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/polyglot"/>
 +
<property name="javax.persistence.jdbc.user" value="polyglot"/>
 +
<property name="javax.persistence.jdbc.password" value="polyglot"/>
 +
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
 +
...
 +
</source>
 +
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
 +
    <persistence-unit name="mongo" transaction-type="RESOURCE_LOCAL">
 +
        <class>model.Order</class>
 +
        <class>model.OrderLine</class>
 +
        <class>model.Address</class>
 +
        <class>model.Customer</class>
 +
        <properties>
 +
            <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
 +
            <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
 +
            <property name="eclipselink.nosql.property.mongo.port" value="27017"/>
 +
            <property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
 +
            <property name="eclipselink.nosql.property.mongo.db" value="polyglot"/>
 
</source>
 
</source>

Revision as of 17:36, 3 April 2013

This example is a variation of the NoSQL Example with some of the entities mapped to MongoDB and some to relational. The object model and the configuration for MongoDB is described in that example and is not repeated here.

Domain Model

Two of the entities from the NoSQL example have been altered so that they are mapped to a relational database (MySQL in the this case). Those two entities are Discount and Product:

@Entity
public class Discount {
	@Id
	@GeneratedValue
	private int id;
	private float amount;
...
@Entity
public class Product {
	@Id
	@GeneratedValue
	private int id;
	private String description;
...

The Order and OrderLine, mapped to MongoDB, have relationships to Discount and Product, respectively.

@Entity
@NoSql(dataFormat=DataFormatType.MAPPED)
public class Order {   
    @Id // Use generated OID (UUID) from Mongo.
    @GeneratedValue
    @Field(name="_id")
    private String id;
    @Basic
    private String description;
    @OneToOne(cascade={CascadeType.REMOVE, CascadeType.PERSIST})
    private Discount discount;
 ...
@Embeddable
@NoSql(dataFormat=DataFormatType.MAPPED)
public class OrderLine implements Serializable {
    @Basic
    private int lineNumber;
    @OneToOne(cascade={CascadeType.REMOVE, CascadeType.PERSIST})
    private Product product;
...

Composite Persistence Unit

Composite persistence units provide a way to combine entities associated with different data sources into a single persistence unit. Whether the data source is relational or non-relation is irrelevant. Users of the composite persistence unit are unaware that they are working with a composite and that entities returned from queries or through relationship navigation may be coming from different data sources--they simple interact with a set entities.

In this example there are two component persistence units: "relational-pu" containing Discount and Product and "nosql-pu" containing the remaining classes. The persistence.xml for each of these persistence units lists their classes and associates them with the configured data source:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
	<persistence-unit name="RelationalPU" transaction-type="RESOURCE_LOCAL">
		<class>relational.model.Product</class>
		<class>relational.model.Discount</class>
		<properties>
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/polyglot"/>
			<property name="javax.persistence.jdbc.user" value="polyglot"/>
			<property name="javax.persistence.jdbc.password" value="polyglot"/>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
...
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="mongo" transaction-type="RESOURCE_LOCAL">
        <class>model.Order</class>
        <class>model.OrderLine</class>
        <class>model.Address</class>
        <class>model.Customer</class>
        <properties>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
            <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
            <property name="eclipselink.nosql.property.mongo.port" value="27017"/>
            <property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
            <property name="eclipselink.nosql.property.mongo.db" value="polyglot"/>

Back to the top