what is the use of @PersistentContext and @Statele

2019-05-31 21:50发布

问题:

I am fairly new to using JAX-RS. The tutorials I went through made it really simple by showing how to make GET/POST/DELETE requests. But did not go through advanced annotations. Now I am reading the Java EE 7 essentials book. I am confused with many new annotations that I see here. I tried to find the utility of these annotations. but I did not understand. I have always found SO answers to be easily understandable for beginners.

Here is the code from github:

Employee.Java

@Entity
@Table(name = "REST_DB_ACCESS")
@NamedQueries({
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
})
@XmlRootElement
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(length=40)
private String name;
public Employee() { }
public Employee(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name + " " + id;
}
@Override
public boolean equals(Object obj) {
if (null == obj)
return false;
if (!(obj instanceof Employee))
return false;
Employee that = (Employee)obj;
if (that.name.equals(this.name) && that.id == this.id)
return true;
else
return false;
}
@Override
public int hashCode() {
return Objects.hash(this.id, this.name);
}
}

EmployeeResource.Java

 @Path("employee")
    @Stateless
    public class EmployeeResource {
    @PersistenceContext
    EntityManager em;
    @GET
    @Produces("application/xml")
    public Employee[] get() {
    return em.createNamedQuery("Employee.findAll", Employee.class).getResultList().toArray(new Employee[0]);
    }
    }

MyApplication.java

@javax.ws.rs.ApplicationPath("webresources")
public class MyApplication extends Application {
}
  1. What is the use of EmployeeResource class? Is that a design pattern? I could have done this using DAO access in get() method?
  2. what does @PersistentContext and @Stateless mean? I did search for these the annonations in google. but I am not convinced that I understood it
  3. what is the use of Application Class? Is it always needed? In the tutorial I went through, no mention was made about Application class. On the same token, what does @ApplicationPath annotation mean?

Thank you for your time!

回答1:

  1. What is the use of EmployeeResource class? Is that a design pattern? I could have done this using DAO access in get() method?

    The EmployeeResource class represents your RESTful service. From it you are going to expose different methods using @GET, @PUT, @POST, @DELETE etc.

  2. what does @PersistentContext and @Stateless mean? I did search for these the annonations in google. but I am not convinced that I understood it

    @PersistenceContext is a JPA annotation. JPA covers the conversion of database data to/from a Java domain model. Since RESTful services generally represent CRUD (create, read, update, delete) operations with Java domain objects as parameters it is very convenient to use JPA to do the actual persistence operations.

    @Stateless is a EJB (Session Bean) annotation. It means that the bean cannot maintain any sort of state between calls.

  3. what is the use of Application Class? Is it always needed? In the tutorial I went through, no mention was made about Application class. On the same token, what does @ApplicationPath annotation mean?

    It is a useful way to pull in pieces that aren't annotated with @Provider. One useful annotation is the @ApplicationPath annotation (see: http://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html). This sets the first part part of the path, this is followed by the @Path set on the resource class, which is finally followed by the @Path set on the REST operation itself.