DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Jakarta NoSQL 1.1: Advancing Polyglot Persistence for Jakarta EE 12
  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era

Trending

  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Batching Strategies: Throughput vs Latency in Practice
  • How to Format Articles for DZone
  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. NoSQL with JPA

NoSQL with JPA

By 
Hüseyin Akdoğan
DZone Core CORE ·
Aug. 06, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
31.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
NoSQL Web application

Opinions expressed by DZone contributors are their own.

Related

  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Jakarta NoSQL 1.1: Advancing Polyglot Persistence for Jakarta EE 12
  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook