Creating a course entity bean and a JPA factory

If you are not familiar with JPA, refer to the JPA concepts section in Chapter 4, Creating JEE Database Applications.

We will now create Course.java in the packt.book.jeeeclipse.wildflyswarm.coursemanagement.rest package:

package packt.book.jeeeclipse.wildflyswarm.coursemanagement.rest;

// skipping imports to save space

@Entity
@Table(name=""Course"")
@NamedQuery(name="Course.findAll", query="SELECT c FROM Course c")
public class Course implements Serializable {
private static final long serialVersionUID = 2550281519279297343L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@NotNull
@Column(name="name")
private String name;

@Min(1)
@Column(name="credits")
private int credits;

// skipping getter and setters to save space
}

This is a simple JPA entity class with appropriate annotations. We need to tell JPA that this is a managed bean. To do this, open persistence.xml and in the General tab of the editor, click the Add button in the Managed Classes section. Add the Course entity class to the list.

Create a JPA EntityManagerFactory class called CourseManagementJPAFactory:

package packt.book.jeeeclipse.wildflyswarm.coursemanagement.rest;

// skipping imports to save space

@ApplicationScoped
public class CourseManagementJPAFactory {
private EntityManager _entityManager;

public EntityManager getEntityManager() {
if (_entityManager != null) return _entityManager;

EntityManagerFactory factory = Persistence.createEntityManagerFactory("coursemanagement");

_entityManager = factory.createEntityManager();

return _entityManager;
}
}

In this class, we are creating an instance of EntityManager from EntityManagerFactory. Note that name passed to the Persistence.createEntityManagerFactory method is the same as the name we specified in persistence.xml.

Finally, we will create the main class, called CourseManagementEndpoint, and also the REST endpoint function to handle the /course_management/courses URL path:

package packt.book.jeeeclipse.wildflyswarm.coursemanagement.rest;
// skipping imports to save space

@ApplicationScoped
@Path("/course_management")
public class CourseManagementEndpoint {
@Inject
private CourseManagementJPAFactory jpaFactory;

@GET
@Path("/courses")
@Produces(MediaType.APPLICATION_JSON)
public List<Course> doGet() {
EntityManager entityManager = jpaFactory.getEntityManager();
TypedQuery<Course> courseQuery = entityManager.createNamedQuery("Course.findAll", Course.class);
List<Course> courses = courseQuery.getResultList();
return courses;
}
}

If the application is not already running, right-click on the project in Project Explorer and select Run As | Maven build. Open http://localhost:8080/course_managment/courses in the browser and you should see a JSON list of courses in the database.

To change the default server port from 8080 to any other port number, say 8000, set the swarm.http.port=8000 environment variable. You can set this in the run configuration for the project (select Run | Run Configurations from the main menu and look for the configuration for your project in the Maven Build section):

Figure 12.5: Set the environment variable in the run configuration

Click on the Environment tab and add the environment variable and its value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.119.81