Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be 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/JPA/NoSQL"

(Ordering Example)
(Ordering object model)
Line 32: Line 32:
 
     private List<OrderLine> orderLines = new ArrayList<OrderLine>();
 
     private List<OrderLine> orderLines = new ArrayList<OrderLine>();
 
     private Customer customer;
 
     private Customer customer;
 +
    ...
 +
}
 +
public class OrderLine implements Serializable {
 +
    private int lineNumber;
 +
    private String description;
 +
    private double cost = 0;
 +
    ...
 +
}
 +
public class Address implements Serializable {
 +
    private String street;
 +
    private String city;
 +
    private String province;
 +
    private String country;
 +
    private String postalCode;
 +
    ....
 +
}
 +
public class Customer implements Serializable {
 +
    private String id;
 +
    private String name;
 
     ...
 
     ...
 
}
 
}
 
</source>
 
</source>
 +
 +
MongoDB stores data as BSON (binary JSON) documents.  The first decision that must be made is how to store the objects.  Normally each independent object would compose a single document, so a single document could contain Order, OrderLine and Address.  Since customers can be shared amongst multiple orders, Customer would be its own document.
 +
 +
The next step is to map the objects.  Each root object in the document will be mapped as an @Entity in JPA.  The objects that are stored by embedding within their parent's document are mapped as @Embeddable.  This is similar to how JPA maps relational data, but in NoSQL embedded data is much more common because of the hierarchical nature of the data format.  In summary, Order and Customer are mapped as @Entity, OrderLine and Address are mapped as @Embeddable.
  
 
[[Category:EclipseLink/Example/JPA|NoSQL]]
 
[[Category:EclipseLink/Example/JPA|NoSQL]]

Revision as of 11:34, 26 March 2012

NoSQL

NoSQL is a classification of database systems that do not conform to the relational database or SQL standard. They have various roots, from distributed internet databases, to object databases, XML databases and even legacy databases. They have become recently popular because of their use in large scale distributed databases in Google, Amazon, and Facebook.

There are various NoSQL databases including:

  • Mongo DB
  • Oracle NoSQL
  • Cassandra
  • Google BigTable
  • Couch DB

As of EclipseLink 2.4, EclipseLink has added JPA support for NoSQL databases, initially with support for MongoDB and Oracle NoSQL.

EclipseLink's NoSQL support allow the JPA API and JPA annotations/xml to be used with NoSQL data. EclipseLink also supports several NoSQL specific annotations/xml including @NoSQL that defines a class to map NoSQL data.

EclipseLink's NoSQL support is based on previous EIS support offered since EclipseLink 1.0. EclipseLink's EIS support allowed persisting objects to legacy and non-relational databases. EclipseLink's EIS and NoSQL support uses the Java Connector Architecture (JCA) to access the data-source similar to how EclipseLink's relational support uses JDBC. EclipseLink's NoSQL support is expendable to other NoSQL databases, through the creation of an EclipseLink EISPlatform class and a JCA adapter.

Ordering Example

This example shows how to map and persist an Ordering system object model to a MongoDB NoSQL database.

The source for the example can be found here, or from the EclipseLink SVN repository.

The ordering system consists of four classes, Order, OrderLine, Address and Customer. The Order has a billing and shipping address, many order lines, and a customer.

Ordering object model

public class Order implements Serializable {
    private String id;
    private String description;
    private double totalCost = 0;
    private Address billingAddress;
    private Address shippingAddress;
    private List<OrderLine> orderLines = new ArrayList<OrderLine>();
    private Customer customer;
    ...
}
public class OrderLine implements Serializable {
    private int lineNumber;
    private String description;
    private double cost = 0;
    ...
}
public class Address implements Serializable {
    private String street;
    private String city;
    private String province;
    private String country;
    private String postalCode;
    ....
}
public class Customer implements Serializable {
    private String id;
    private String name;
    ...
}

MongoDB stores data as BSON (binary JSON) documents. The first decision that must be made is how to store the objects. Normally each independent object would compose a single document, so a single document could contain Order, OrderLine and Address. Since customers can be shared amongst multiple orders, Customer would be its own document.

The next step is to map the objects. Each root object in the document will be mapped as an @Entity in JPA. The objects that are stored by embedding within their parent's document are mapped as @Embeddable. This is similar to how JPA maps relational data, but in NoSQL embedded data is much more common because of the hierarchical nature of the data format. In summary, Order and Customer are mapped as @Entity, OrderLine and Address are mapped as @Embeddable.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.