OpenUI5 and OData data bindings with MariaDB – CRUD Full Example- Part 1

OpenUI5 is the open source version of the powerful web MVC framework SAPUI5 created by SAP AG. OpenUI5 is based on web standards (HTML5, CSS, OData, XML, jQuery). The backend is linked using OData, JSON, XML and other data formats. Last but not least OpenUI5 have the same principles created for SAPUI5 (ROLE – BASED, ADAPTIVE, COHERENT, SIMPLE and DELIGHTFUL). For that reason OpenUI5 is a great option to build powerful solutions.

In this tutorial we will create a CRUD (Create, Read, Update and Delete) solution using MariaDB v10.1.21 as database, JPA v2.1, Apache Olingo v2.0.8 and Tomcat v7.0.56 to create an OData service, OpenUI5 v1.46.8 as Front End and Tomcat v7.0.56 as application server all included on XAMPP v5.6.30.

This first part include the installation and configuration of the environments, the data table creation in the database and expose the data table as OData service. The second part will include the OpenUI5 front end with the CRUD concepts implemented.

Prerequisites:

Step-by-Step:

MariaDB set up

The first step is start up your Apache, MySQL and Tomcat services. 1. Click on Start button of Apache, MySQL and Tomcat. 2. Click on Admin button of MySQL.

01

In phpMyAdmin portal, open test database and click on New to create a new table.

02

  • Set People as table name.
  • Set Phone as field name, type = Varchar, Length = 20, Index = Primary.
  • Set Name as field name, type = Varchar, Length = 20.
  • Set LastName as field name, type = Varchar, Length = 20.
  • Set Email as field name, type = Varchar, Length = 50.
  • Click on Save (Guardar) button

03

JPA OData Service

Download JPA Libraries Dependencies and extract the files.

13

To create an OData service is required to connect with the data base. In eclipse IDE select Windows / Show View / Data Source Explorer.

10

Right click on Database Connection folder and click on New.

11

Find MySQL as Connection Type, Set People_DB as Name. Click on Next.

12

To create a connection driver click on New Driver Definition button.

14

If the jar driver file doesn’t exist in eclipse then you will see the “Unable to locate JAR”. Select MySQL JDBC Driver 5.1 and go to Jar List tab.

15

Remove any driver file that exist by default and add the mysql-connector-java-5.1.18.jar that was downloaded from JPA Libraries Dependencies and press OK.

16

Set the parameters:

  • Database: test
  • URL: jdbc:mysql://localhost:3306/test
  • User: root

Press on Test Connection button. You should see a success ping message. Click on Finish button.

17

In eclipse IDE create a new projectRight click on Project Explorer view / New / Dynamic Web Project.

04

  • Set People_OData as project name.
  • Click on New Runtime
  • Select Apache Tomcat (select de version that you have installed with XAMPP).
  • Click on Next.

05

Click on Browser button and find your Tomcat installed with XAMPP and click on Finish.

06

Click on Modify, Check the JPA project face. Click on OK and click on Next. Click Next in Java – src screen too.

07

  • Select EclipseLink 2.5.x as Platform.
  • Select Disable Library Configuration as JPA implementation.
  • Select Annotated classes must be listed in persistence.xml as Persistent class management. Click on Next.

08

Check the option Generate web.xml deployment descriptor and click on Finish button.

09

Copy and paste into People_OData/WebContent/WEBINF/lib folder all the jar files that were downloaded from JPA Libraries Dependencies. Right click on People_OData/WebContent/WEBINF/lib folder and click on Validate option.

18

In eclipse menu select Project / Clean and select People_OData project and press OK.

19

We need to create a new JPA Entities from Tables file. Right click on People_OData/ New / JPA Entities from Tables.

20

  • Select People_DB as connection.
  • People as table.
  • Check List generated classes in persistence.xml
  • Click on Next. In Table Associations screen click on Next too.

21

In Customize Defaults options, check Always generate optional JPA annotations and DDL parameters option and click on Finish.

22

Open your persistence.xml and select the connection tab.

  • Set Resource Local as transaction type.
  • Click on Populate from connection to get the values from People_DB. Save the file.

23

Create a new class:

  • Set main as package.
  • Set PeopleServiceFactory as name.
  • Set ODataJPAServiceFactory as superclass.

24

Replace all the code in the class PeopleServiceFactory with this code.

package main;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

public class PeopleServiceFactory extends ODataJPAServiceFactory {

private static final String PERSISTENCE_UNIT_NAME = “People_OData”;

@Override
public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
ODataJPAContext oDatJPAContext = this.getODataJPAContext();
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
oDatJPAContext.setEntityManagerFactory(emf);
oDatJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
return oDatJPAContext;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}

Replace all the code in web.xml file with all this code.

 

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
xmlns=”http://java.sun.com/xml/ns/javaee
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
id=”WebApp_ID” version=”3.0″>
<display-name>People_OData</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ODataServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>
</init-param>
<init-param>
<param-name>org.apache.olingo.odata2.service.factory</param-name>
<param-value>main.PeopleServiceFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ODataServlet</servlet-name>
<url-pattern>/peopleService.svc/*</url-pattern>
</servlet-mapping>
</web-app>

Right click on People_OData project and select Run As / Run on Server.

25

Select your localhost server and Finish.

26

To test the OData service put this url in your internet browser: http://localhost:8080/People_OData/peopleService.svc/

27

The service is running very well.

spanish-version

Advertisements

3 thoughts on “OpenUI5 and OData data bindings with MariaDB – CRUD Full Example- Part 1

  1. Pingback: OpenUI5 and OData data bindings with MariaDB – CRUD Full Example- Part 2 | Innovators at Heart

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s