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:
@Entity
public class Employee {
@Id
private Long id;
private String name;
//getters and setters
}
EJB bean:
@Stateless
public class EmployeeService {
//The entity manager will be injected automatically
@PersistenceContext
private EntityManager em;
public Employee findEmployeeById(Long id) {
return em.find(Employee.class, id);
}
}
JSF controller (let's assume it's CDI-bean):
@Named
@SessionScoped
public class EmployeeController implements Serializable {
//using CDI @Inject annotation empService will be initialized automatically
@Inject
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;
}
}
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
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.
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:
EJB bean:
JSF controller (let's assume it's CDI-bean):
xhtml page:
Update Some books that might help:
These are quite popular and cover a lot of ground.
You may also like to have a look on:
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
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:
This quote in particular is wrong. All depends on where you put your
@Id
annotation. The specification says: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!):
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.