Software Development

Spring Boot, JPA/Hibernate for Java EE 6 Java Persistence API Developer Certified Expert 1Z0-898

If you are planning to take Java EE 6 Java Persistence API Developer Certified Expert (1Z0-898), this post is for you. It demonstrates how to create a sample application using Spring Boot, and JPA/Hibernate to aid in your study.

[wp_ad_camp_1]

Install MySQL

NOTE: It is possible to use an embedded H2 database that comes with Spring Boot. However, you may need a database that keeps data even after your machine has been shutdown. Therefore, I would recommend using non-in-memory databases like MySQL.

Before anything else, we need a RDBMS. We need a database to connect to to read/write data from/to. This post, however, does not demonstrate how to install MySQL.

Take note of the username and password used during the installation. You’ll be using them to configure the application to point to your database.

Spring Initialzr

Visit http://start.spring.io and provide similar information as shown below. Then, click on the “Generate Project” button to generate your initial source code files.

Notice that we only selected JPA and MySQL.

Import to Eclipse

Once you imported the project into Eclipse and built it, it should look something like this.

Update application.properties

Update your application.properties with these values. Here we have a database running in the same machine and the database name is jpahibernatedemo.

Replace yourusername and yourpassword with valid user name and password, respectively.

[wp_ad_camp_2]

Where’s my EntityManagerFactory?

With Spring Boot, you can retrieve the EntityManagerFactory as a bean from the ApplicationContext. We could use @Autowired but we are just running things in the main method to keeps things very simple.

From your EntityManagerFactory, you can now get the EntityManager and EntityTransaction.

[wp_ad_camp_3]

There you go! Now, you can JPA concepts you’ve learned hands-on.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like