Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

NoSQL with JPA

DZone's Guide to

NoSQL with JPA

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

EclipseLink, reference implementation of JPA, has JPA support for NoSQL databases (MongoDB and Oracle NoSQL) as of the version 2.4. In this tutorial we will discuss the use of MongoDB database with the JPA support of EclipseLink. The transaction previously done using the console and native java driver will be done in a web application with the help of EclipseLink.


Tools and technologies used in the sample application are as follows:


  • MongoDB version 2.4.1
  • MongoDB Java Driver version 2.11.1
  • JSF version 2.2
  • PrimeFaces version 3.5
  • EclipseLink version 2.4
  • Jetty 7.x Maven Plugin
  • JDK version 1.7
  • Maven 3.0.4



Project Dependencies

<dependency>
   <groupId>org.glassfish</groupId>
   <artifactId>javax.faces</artifactId>
   <version>2.2.0-SNAPSHOT</version>
</dependency>

<dependency>
   <groupId>org.primefaces</groupId>
   <artifactId>primefaces</artifactId>
   <version>3.5</version>
</dependency>

<dependency>
   <groupId>org.primefaces.themes</groupId>
   <artifactId>bootstrap</artifactId>
   <version>1.0.10</version>
</dependency>

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>org.eclipse.persistence.jpa</artifactId>
   <version>2.4.0-SNAPSHOT</version>
</dependency>

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>org.eclipse.persistence.nosql</artifactId>
   <version>2.4.0-SNAPSHOT</version>
</dependency>

<dependency>
   <groupId>jboss</groupId>
   <artifactId>jboss-j2ee</artifactId>
   <version>4.2.2.GA</version>
</dependency>

<dependency>
   <groupId>org.mongodb</groupId>
   <artifactId>mongo-java-driver</artifactId>
   <version>2.11.1</version>
</dependency>

<dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3</version>
</dependency>


Entity Class


@Entity
@NoSql(dataFormat=DataFormatType.MAPPED)
public class Article implements Serializable {

    public Article() { 
    }

    @Id
    @GeneratedValue
    @Field(name="_id")
    private String id;

    @ElementCollection
    private List<Categories> categoryLists = new ArrayList<Categories>();

    @Basic
    private String title;

    @Basic
    private String content;

    @Basic
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;

    @Basic
    private String author;

    @ElementCollection
    private List<Tags> tagLists = new ArrayList<Tags>();


@NoSQL notation sets the data format and type and maps the NoSQL data.  Because of using MongoDB in our sample application and documents in MongoDB stored in BSON format, MAP is used as data type. @ElementCollection notation maps the embedded collection into the parent document. Because more than one category and tag associated with an article would be a matter in our sample application, we map them as an element collection.


Embedded Objects


@Embeddable
@NoSql(dataFormat=DataFormatType.MAPPED)
public class Categories implements Serializable {

    @Basic
    private String category;


@Embeddable
@NoSql(dataFormat=DataFormatType.MAPPED)
public class Tags implements Serializable {

    @Basic
    private String tag;


We see @Embeddable notation at the top of the Categories and Tags’ class unlike Article entity class. The documents stored in the parent document are mapped with this notation. Please note that embedded objects do not need unique field.


persistence.xml


<?xml version="1.0" encoding="UTF-8" ?>
<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"
        version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="kodcuMongo" transaction-type="RESOURCE_LOCAL">
        <class>com.kodcu.entity.Article</class>
        <class>com.com.kodcu.entity.Categories</class>
        <class>com.kodcu.entity.Tags</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="kodcu"/>
            <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>


CRUD Operations 
index.xhtml


<h:form id="articleForm">
            <p:growl id="growl"/>
            <h:panelGrid columns="2" cellpadding="2" cellspacing="2">
                <p:panel styleClass="registrationForm" header="Registration Form">

                    <h:panelGrid id="registrationPanelGrid" columns="2" cellpadding="2" cellspacing="2">
                        <h:outputLabel value="Category"/>
                        <p:inputText value="#{myBean.categories}" styleClass="inputTextWidth"/>

                        <h:outputLabel value="Title"/>
                        <p:inputText value="#{myBean.article.title}" required="#{param['saveArticle']=='true'}" requiredMessage="The title field is required." styleClass="inputTextWidth"/>

                        <h:outputLabel value="Content"/>
                        <p:inputTextarea value="#{myBean.article.content}" required="#{param['saveArticle']=='true'}" requiredMessage="The content field is required." styleClass="inputTextAreaWidthHeight"/>

                        <h:outputLabel value="Author"/>
                        <p:inputText value="#{myBean.article.author}" required="#{param['saveArticle']=='true'}" requiredMessage="The author field is required." styleClass="inputTextWidth"/>

                        <h:outputLabel value="Tags"/>
                        <p:inputText value="#{myBean.tags}" styleClass="inputTextWidth" />

                    </h:panelGrid>
                </p:panel>

                <p:panel header="Article Directory" styleClass="articleDirectory">
                    <p:dataTable id="articleTable" var="article" value="#{myBean.articleList}" selectionMode="single" 
                                 selection="#{myBean.selectArticle}" paginator="true" paginatorPosition="bottom" rows="5"
                                 rowKey="#{article.id}">

                        <p:ajax event="rowSelect" listener="#{myBean.articleSelect}" 
                                update=":articleForm:registrationPanelGrid :articleForm:removeArticle"/>

                        <p:column width="200">
                            <f:facet name="header">
                                <h:outputLabel value="Title"/>
                            </f:facet>
                            <h:outputLabel value="#{article.title}"/>
                        </p:column>

                        <p:column width="350">
                            <f:facet name="header">
                                <h:outputLabel value="Content"/>
                            </f:facet>
                            <h:outputLabel value="#{article.content}"/>
                        </p:column>

                        <p:column width="200">
                            <f:facet name="header">
                                <h:outputLabel value="Author"/>
                            </f:facet>
                            <h:outputLabel value="#{article.author}"/>
                        </p:column>

                        <p:column width="40">
                            <f:facet name="header">
                                <h:outputLabel value="Date"/>
                            </f:facet>
                            <h:outputLabel value="#{article.date}">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </h:outputLabel>
                        </p:column>

                    </p:dataTable>
                </p:panel>

                <h:panelGroup>
                    <p:commandButton id="newArticle" action="#{myBean.initArticle}" value="New" 
                                     update="registrationPanelGrid articleTable removeArticle"/>
                    <p:commandButton id="saveArticle" action="#{myBean.saveArticle}" value="Save" 
                                     update="registrationPanelGrid articleTable growl">
                        <f:param name="saveArticle" value="true"/>
                    </p:commandButton>
                    <p:commandButton id="removeArticle" disabled="#{null == myBean.selectArticle}" action="#{myBean.removeArticle}" 
                                     value="Remove" update="registrationPanelGrid articleTable @this"/>
                </h:panelGroup>
            </h:panelGrid>

        </h:form>


MyBean.java


public void saveArticle() {

    em.getTransaction().begin();

    if(null == article.getId())
       em.persist(article);
    else
       em.merge(article);

    em.getTransaction().commit();
}

public void removeArticle() {

    em.getTransaction().begin();
    em.remove(selectArticle);
    em.getTransaction().commit();

}


6. Demo Application




Real content above and the demo application, can be accessed at  NoSQL with JPA

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:
java ,nosql ,bigdata ,jpa ,tool ,tips and tricks ,tools & methods ,mongodb

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}