Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

JRebel and Java EE – Let’s be productive series: JPA Entities

Welcome to the next segment in the Let’s be Productive with Java EE series. We introduced the series as a means for diving deeper into the examples showcased in our JRebel webinar with Red Hat and more specifically adding HttpServlets with the @WebServlet annotation. Fire up your IDE, application server, and let’s get productive.

In this segment, let’s take our application from before and we’ll add a new JPA entity, without restarting the application thanks to JRebel. As with the previous post, we’ll use Red Hat’s WildFly 8.2, but this code should work with any Java EE 6 or 7 compliant application server. Our application already has a Student entity and accompanying REST endpoint, those are great to play around with to get a sense of what you can do without too much code.

Here are links to the rest of the series:
Part 1: Creating new Servlets
Part 3: CDI Bean Injection


We’re going to add a new entity, User, and modify our HelloServlet from before to randomly generate new Users and then display them in the response. This isn’t best practices to use an EntityManager in HttpServlet, but we’re focusing on JPA for this post.

To get started, let’s start the application and verify that the JRebel banner is in the console of your application server. You should see something like this:

2015-04-13 11:50:06 JRebel:  
2015-04-13 11:50:06 JRebel:  JRebel Agent 6.1.2 (201504061000)
2015-04-13 11:50:06 JRebel:  (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2015-04-13 11:50:06 JRebel:  

Let’s go to our application’s HelloServlet in a browser http://localhost:8080/HelloJavaEE7/HelloServlet/

Just as before, it shows us a welcome message
Initial output of the HelloServlet showing the JRebel welcome message

Now, while the application is still up and running, let’s add a new JPA entity with Eclipse’s wizard, we’ll name it User and give it an id field, with the type long and make it the primary key.

Eclipse's context menu showing the JPA Entity options

We’re left with a simple User class with a single field, getters and setters, and a simple constructor. To make this entity class more useful, let’s add a constructor that takes in the id as a parameter, override the toString method, and we should also define a named query we’ll use later. Here’s the named query:

@NamedQuery(name="findAllUsers", query="select u from User u")

To prove that this new entity works, we’ll use the new entity in our application. Once again, this is not best practice and is just a clear way to show the new code works without having to include too much superfluous code. Let’s focus on what’s important here, a working new JPA entity. We’re going to inject an EntityManager and a UserTransaction resource into our HelloServlet, then the GET request’s response will create a new User object, persist it into the database, and then run the named query to show us all of the items that we’ve persisted.

First, let’s add our EntityManager, we need the UserTransaction as well because we need to persist to the database from inside a transaction and this is a “safe” way to handle it.

private EntityManager em;

@Resource private UserTransaction ut;

Then, replace the body of the doGet method with:


TypedQuery<User> query = em.createNamedQuery("findAllUsers", User.class);
final List<User> users = query.getResultList();
StringBuffer sb = new StringBuffer();
for (User testUser : users) {
  sb.append(testUser.toString() + "\n");

and persistAUser should be defined this like:

private void persistAUser(){
    try {
      double tempID = Math.random();
      int id = (int) (tempID*100);
      User tempUser = new User(id+1);
    } catch (Exception e) {

When we hit save, eclipse compiles the class, and we see the JRebel reloaded the class in the console:

JRebel: Reloading class 'org.sample.HelloServlet'.

Now, when we hit the servlet in our browser:

The new entity is working, the servlet is persisting and displaying the new User objects

This shows that our new JPA entity is working great, our HelloServlet class is properly persisting to the database, and we didn’t have to stop and rebuild the application. Thanks to the class reloading power of JRebel, we were able to reload code changes on the fly. Java developers on average waste over one full work month per year waiting for redeploys and rebuilds of application servers and apps. Take that time back with JRebel and be more productive with Java EE.

Now, that’s productive! Try JRebel for yourself in your app and see how you can save over one full work-month per year!



If you have any questions you can find me on twitter at @akoblentz or leave a comment below!