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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Top NoSQL Databases and Use Cases
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java

Trending

  • When MySQL, PostgreSQL, and Oracle Argue: Doris JDBC Catalog Acts as the Peacemaker
  • 10 Predictions Shaping the Future of Web Data Extraction Services
  • Top NoSQL Databases and Use Cases
  • Understanding N-Gram Language Models and Perplexity
  1. DZone
  2. Data Engineering
  3. Data
  4. NoSQL with JPA

NoSQL with JPA

By 
Hüseyin Akdoğan user avatar
Hüseyin Akdoğan
DZone Core CORE ·
Aug. 06, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
31.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Top NoSQL Databases and Use Cases
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Handling Embedded Data in NoSQL With Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: