Pro JPA 2: Mastering the Java™ Persistence API (Expert's Voice in Java Technology)

Category: Programming
Author: Mike Keith, Merrick Schincariol
All Stack Overflow 8


by anonymous   2017-08-20

You're asking too general questions. Grab some decent book on Java EE (there are tons of them). For better understanding of dependency injection, I'd recommend official Weld documentation. However here is a typical example aimed to show you how to connect different Java EE layers:

JPA Entity:

public class Employee {

  private Long id;

  private String name;

  //getters and setters

EJB bean:

public class EmployeeService {

  //The entity manager will be injected automatically  
  private EntityManager em;

  public Employee findEmployeeById(Long id) {
    return em.find(Employee.class, id);


JSF controller (let's assume it's CDI-bean):

public class EmployeeController implements Serializable {

  //using CDI @Inject annotation empService will be initialized automatically
  private EmployeeService empService;

  //this method can be called from .xhtml page
  public String obtainEmployeeName(Long id) {
    String empName = "";
    Employee emp = empService.findEmployeeById(id);
    if (emp != null) {
      empName = emp.getName();
    return empName;

xhtml page:

<h:outputText value="#{employeeController.obtainEmployeeName(3)}" />

Update Some books that might help:

  • Pro JPA 2: Mastering the Java Persistence API
  • Core JavaServer Faces (3rd Edition)
  • Beginning Java EE 6 with GlassFish 3
  • These are quite popular and cover a lot of ground.

    by anonymous   2017-08-20

    You may also like to have a look on:

    • JSf or Spring (Frameworks)
    • Java persistance API
    • Java messaging Service

    I dont know whether the last two technologies are supported in J2EE or not, but they are supported in JavaEE6. A good overview about the these is offered by this book: JavaEE6 Tutorial

    For java persistance API, You may want to look at this book: Pro JPA 2

    by anonymous   2017-08-20

    I see that other comments has misguided you so I feel myself obliged to elaborate on this issue a bit, even though I can't give you a scientific and complete answer. @vcetinick wrote the current accepted answer:

    You may find that you may be able to get away [..] from the persistence side of things.

    This quote in particular is wrong. All depends on where you put your @Id annotation. The specification says:

    If the entity has field-based access, the persistence provider runtime accesses instance variables directly.

    Thus you are not required in any way to provide a setter or getter. Because you annotated your field and not a getter method (annotating the setter method will be ignored and have no bearing).

    However, if you write a getter method, and annotated this method with your @Id annotation instead of your field, then we would tell our persistence provider to access our field through the accessor (getter) and mutator (setter) methods and not use reflection. In that case, both a getter and a setter should be present. The book Pro JPA 2: Mastering the Java™ Persistence API writes on page 71 (bold markup by me!):

    When property access mode is used, the same contract as for JavaBeans applies, and there must be getter and setter methods for the persistent properties. The type of property is determined by the return type of the getter method and must be the same as the type of the single parameter passed into the setter method. Both methods must be either public or protected visibility.

    Therefore, I usually annotate my id field, and write both a setter and getter method, but the setter method I give protected access. I just don't want any other pieces of code to have easy write access to such an important field. I don't know if this would cause problems in other domains. I'm no expert. But I don't find any rationale either as to why not setup an id attribute in this way. See also the Netbeans forums.