Sunteți pe pagina 1din 38

Java: Hibernate, JSF Primefaces and MySQL [Part 1]

jeanCedron
May 25 2014
134
Step-by-step: Creating a Java project with MVC pattern

Note: All content is of my authorship

Hello as they are, this is the first tutorial that I make to Taringa. For all the people who are interested in programming this little document will be a lot of help. In the
development of the project will use JSF Primefaces for the view of the application, hibernate as ORM, MySQL as DBMS, and Netbeans as development IDE.

Basic concepts:

we Begin by giving you know a little about the concepts mentioned above:

JSF: The JSF is a framework for web applications in Java, allows you to use beans to control the views of the application and also allows you to use a view JSP. As a
component of JSF to use Primefaces that makes it easy to create our applications.

ORM: The ORM is a mapping from a relational database into objects through an engine of persistence, in this case it will use the Hibernate, which is developed for
java. For example, suppose we have a table Person in our database, which will cause Hibernate is to create a class called Persona.java, in which each object of that class
is a log table and a file Persona.xml, which define the table in a syntax predefined by the engine of persistence.

DBMS: A DBMS (database management System data) is a term that is sure many of you know, however I'll explain very briefly: it Is a set of programs that allow you to
modify, store, delete, extract data from a database. In this project, we will use the MySQL as it is open source.

IDE: An IDE (integrated development Environment) is a programming environment in which a project is developed, in this case we will use Netbeans in their most
current version: 8.0.

Download of tools:

First, the development IDE, Netbeans 8.0:

Download Netbeans

To download the Netbeans, simply access the link and select the "All" option with support for all the technologies that we will use throughout the project.

We continue with the MySQL community, which is the comprehensive suite of MySQL, it is necessary to install this suite since there is design the model of the database,
it will generate the script of the same, will run the server, and in addition, the installer automatically provides you with all of the available connectors. This does not
have to import again the connector from MySQL to our project.

Download MySQL Community

download MySQL it is necessary to choose the platform, click on Download and select No thanks, just start my download to not have to create an account in Oracle.
Installation tools:

At the time of install the tools in netbeans 8 install all that apply and add the servers Glassfish and Tomcat in case they are deselected.

When you install MySQL, install everything by default by setting the port to 3306 and with the credentials: "root" for user and "1234" for password. These settings I put
it to everyone to follow the standard of the project, it is your decision to change these login credentials for greater security.

Development of the project:

First we'll start with the database, open MySQL Workbench, which comes in the suite that is installed by the MySQL community, we go to File -> New Model
Rename your database (schema) and then we will select Add diagram.

We will add a new table called person, we will add the field respondentid and select AI so that it is an AutoIncrement field, and the fields first name and last name as
Varchar of 45 characters.

We then proceed to export the SQL Script of the database, go to File -> Export -> Forward Engineer SQL Create Script.
We select the first and the last option, the first one for that, in the case that there are already tables created to delete and re-create, and the last in case let's make
some insert in the workbench, however in the tutorial we will not do that but we'll leave turned on. We click Next and then Finish.

We now proceed to lift the server MySQL, we are going to the main screen, select the Local instance mysql and enter the credentials with which you installed the
program.

Once inside we create a new database (Schema) and we name it as we name our schema in the model designed, then we open the SQL script that export it and execute
it with the symbol of lightning.
Once that is all ready in the database, we continue with the project itself.

Then we will open the development IDE and select File -> New project
Select JAVA Web -> Web Application and we click Next.
We chose a name for our project, we choose the place where will be located and in addition, we will select the option to use a dedicated folder for libraries in our project,
then we click Next.
On the server we leave the default Glassfish 4.0, you can use tomcat, however for the example we will use Glassfish and we also by default JAVA EE 7.
In this window, we select the Framework JavaServer Faces (JSF), in the settings tab, change the /faces/.*xhtml by *.xhtml and in the Component tab select Primefaces,
then we click the More button and we hope you find our library more up-to-date of Primefaces.

In addition, we will select the Framework Hibernate and select New database connection: driver select "MySQL (Connector/J Driver), click next and set the following
tab:
Choose the port-installed: 3306, leave it localhost by default, we chose the name of the database we have created, in my case: bdtutorial. We put the credentials with
which you installed the MySQL and at the end we click on Test Connection. If all goes well we click Next and then Finish.
We click finish, the libraries of the frameworks are imported, and as we ready our project for a start to give way.
We'll start by creating packets to be sorted in our classes: We will use the following packages:

Persistence: In this package we will store the connections in hibernate with our database.

Model: Here we will store the models that generate the hibernate with each one of our tables, in java class and xml format.

Bean: In this package we will keep the beans that interact with our views.

DAO: Here are saved all the accesses that are made to our database.

The file named "hibernate.cfg.xml" it is very important and by default it comes in a <default package>, that file is what we will draw to the package Persistence leaving
the structure as shown in the following image:
In the next part of this post, we will proceed to make a maintenance to the table Person.

To continue with the tutorial, please visit the following link: Click here

Java: Hibernate, JSF Primefaces and MySQL [Part 2]


jeanCedron
May 25 2014
93
Step-by-step: Creating a Java project with MVC pattern

Note: All content is of my authorship,

This is the second part of the tutorial that I'm developing. If you wish to see the first part can enter to this link: Click HERE.
In the previous post we finished building our project with this form:

Before continuing, we will change our hibernate.cfg.xml and add hibernate.show_sql = true and hibernate.current_session_context_class =
thread in the design. being of this form:
We will now continue with the structure of our project, this time we will add the classes to be able to interact with our database. We will start by
the package persistent.

The first thing to do is go to the package persistence, right click and then New -> Other. In this window, select Hibernate and create a
HibernateUtil. We will leave the name as it comes "NewHibernateUtil" and give click in finish.
Within the class created, we'll change a line and add inside the configure() method the location of our hibernate.cfg.xml in this case,
"/Persistencia/hibernate.cfg.xml" being as follows:
In the same way as we created the HibernateUtil, we will create the Hibernate Revenge Engineering Wizard, leave selected the default
Configuration file, and we click Add all>> to add the person table to our project.
With this, we finished and we proceed to continue with the package Model. The package model as mentioned in the previous post you have the
tables in java class and xml file that uses hibernate to access the records. Here, in the same way as we created the above files, we will create
Hibernate Mapping Files and POJO's from Database. We click Next and select JDK 5 Language Features as can be seen in the following image:
If we look at the class person, we will realize that it is a mapping exact of our database, with a field String name and an Integer respondentid.
Now we will continue with the DAO, within this package is everything you need to access our database and do different transactions,
Within this package we will create a PersonaDao that will be an interface with access methods and a class called PersonaDaoImplement that will
implement the interface PersonaDao.

Here you can see a structure of the class PersonaDao, as can be seen, the mostrarPersonas returns a list of Person objects, and insert, modify,
and delete takes as a parameter a person who modify, delete, or insert excuse the redundancy.
Then, we will make the class PersonaDaoImplement in which we will implements the class PersonaDao and we will proceed to modify the
methods of access.
Now we will modify the various methods:

Our method mostrarPersonas shall be as follows:


What is done in this method is to create a Session of type hibernate by accessing our HibernateUtil and create a query that returns all records
from the table Person. The language used by our queries Hibernate is called HQL. Finally we fill a list of Person objects with the result of the
query. Do not forget to put the exception handling in every one of our methods of access to the database.

Our method of inserting, as in the case of modify and delete only vary the action of transaction performed: save, update, and delete. The format
is very similar:
As said, the other two methods are very similar:
With this we conclude the second part, the third part of the tutorial you will be seeing how to display, insert, modify and delete the records from
the Person table.

To continue with the tutorial, please visit the following link: Click here

Java: Hibernate, JSF Primefaces and MySQL [Part 3]


jeanCedron
May 25 2014
94
Step-by-step: Creating a Java project with MVC pattern
Note: All content is of my authorship

to return to the second part of the tutorial, in case you haven't yet read, can use the following link: CLICK HERE

Our project every time is taking more shape, just missing to add classes to the package Bean, and modify our view.
For now this is what we have in our project:

First we'll add a bean to our package, it will be called personaBean and manage the view in which we manage our Person table. To be able to
create this bean, we right click our package Bean, New -> Other, select JavaServer Faces and then JSF Managed Bean. As Scope select View.
Now we'll modify the bean in the following way: first we will create a Person object and then a list of person objects, these objects will create
getters and setters so that they can be accessed from the view. We will also add methods to insert, modify, delete and display, which will access
the established methods in our DAO.

Inside the constructor personaBean() define the following: person = new Person() to stop it from being a null object.

First we'll modify the getPersonas() that returns a list of individuals with the following structure to load the data from the database:
Then we will create methods of which I speak above:

Now we will remove our welcomePrimefaces.xhtml and modify our index.xhtml to make a management to the person table previously created.

To be able to add components in Primefaces it is necessary to visit your official page and check out the showcase, this showcase will display all
of the elements of Primefaces and the correct form of be used, as you can see on the page, each one of these uses a bean.

To access the showcase please go to this link: Click here.


In our index.xhtml, within the tags <h:body> create a form, with the following structure

What is being done here is that for each inputText has a value assigned, that value is an attribute of the object of the Person class that is
located in personaBean, then fill the text field and press the button insert, send those attributes to the object of the bean.

In our view, what would be left is the following:


Then, when entering a new person, this would be recorded in our database:

Now, once you already inserted, we will proceed to show all the people who are registered in our database. For this we will use a DataTable, the
information on this component can be found in the showcase that I brindé at the beginning of the guide.

We add the following form with the DataTable and the various columns of each object of the class Person, as you can see, the component
dataTable requests a list of objects, in this case #{personaBean.people}, and assigned to a variable, in this case: "per" to each one of the
records.
So, our view would be of this form:
In addition, the button in the insert form, add the following attribute: update=":formMostrar" that makes using Ajax, update the table data
without needing to refresh the page:

With this, just missing modify and delete: to Add it to the dataTable a field with a delete button and another edit:

And we create two p:dialog, one for edit and another for delete, as you can see below:
To modify:

The view would be like this when you click on modify button:
To remove:
And the view would be like this when clicking on delete:
With this would conclude the tutorial, I hope you have enjoyed and have been able to understand a little more about hibernate, JSF Primefaces
and Mysql. If you have any questions, you can leave it in comment and I'll answer it when you see it. Thank you very much.

Jean Pierre Kidron

S-ar putea să vă placă și