Bonsoir,
Je suis actuellement sur une application JSF+ JPA qu'il faut gérer via Maven.
Je suis sous NetBeans et j'utilise JSF 2 ( PrimeFaces ) et JPA 2 ( eclipsLink), e glassfish 3.1.1.
J'ai mis en place :
-- une entity beans ( CustomerJava) ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
@Entity
@Table(name = "customer")
public class Customer extends AbstractDomain implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "idcustomer")
private Short idcustomer;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 25)
private String namecustomer;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 25)
@Column(name = "surnamecustomer")
private String surnamecustomer;
public Customer() {
}
public Customer(Short idcustomer) {
this.idcustomer = idcustomer;
}
public Customer(Short idcustomer, String namecustomer, String surnamecustomer) {
this.idcustomer = idcustomer;
this.namecustomer = namecustomer;
this.surnamecustomer = surnamecustomer;
}
//getters + setters pour les variables
} |
-- une DAO (en utilisant le mécanise abstract donc j'ai un AbstractDao et un CustomerDao extends AbstractDao) ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public abstract class AbstractDao<T> {
private Class<T> entityClass;
public AbstractDao(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
} |
CustomerDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
@Stateless
public class CustomerDao extends AbstractDao<Customer> {
@PersistenceContext(unitName = "com.ehnor.avel_avel2_war_1.0-SNAPSHOTPU")
private EntityManager em;
//@EJB
//Customer customer;
@Override
protected EntityManager getEntityManager() {
return em;
}
public CustomerDao() {
super(Customer.class);
}
} |
-- Service(AbstractService et CustomerService extends AbstractService),
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public abstract class AbstractService <T extends AbstractDao , M extends AbstractDomain> {
protected Class<T> daoClass;
protected Class<M> domainClass;
public AbstractService(Class<T> daoClass , Class<M> domainClass) {
this.daoClass = daoClass;
this.domainClass = domainClass;
}
public void createService(T daoClass , M domainClass) {
daoClass.create(domainClass);
}
public void edit(T daoClass , M domainClass) {
daoClass.edit(domainClass);
}
public void remove(T daoClass , M domainClass) {
daoClass.remove(domainClass);
}
public Object find(T daoClass ,Object id) {
return daoClass.find(id);
}
} |
CustomerService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @Stateless
public class CustomerService extends AbstractService <CustomerDao , Customer> {
@EJB
private CustomerDao customerDao;
public CustomerService() {
super(CustomerDao.class , Customer.class);
}
/**
* @return the customerDao
*/
public CustomerDao getCustomerDao() {
return customerDao;
}
/**
* @param customerDao the customerDao to set
*/
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
} |
Et enfin un managedBean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
@ManagedBean
@Stateless
@SessionScoped
public class CustomerEditBean {
@EJB
private CustomerService customerService;
private Customer customer;
List<Customer> listAllCustomers;
/**
* Creates a new instance of CustomerEditBean
*/
public CustomerEditBean() {
}
/**
* @return the customer
*/
public Customer getCustomer() {
return customer;
}
/**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
/**
* @return the customerService
*/
public CustomerService getCustomerService() {
return customerService;
}
/**
* @param customerService the customerService to set
*/
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String createNewCustomer(Customer customer)
{
customerService.getCustomerDao().create(customer);
return "home";
}
public String modifyNewCustomer(Customer customer)
{
customerService.getCustomerDao().edit(customer);
return "home";
}
public String deleteCustomer(Customer customer)
{
customerService.getCustomerDao().remove(customer);
return "home";
}
public String findCustomer(Customer customer)
{
this.customer = customerService.getCustomerDao().find(customer.getIdcustomer());
return "home";
}
public String findAllCustomers()
{
this.listAllCustomers = customerService.getCustomerDao().findAll();
return "home";
}
} |
et dans ma page jsf quand je fais
customerBean.customer.namecustomer
, je me prends une exception Target Unreachable, 'null' returned null
Je vous remercie par avance.
NB : Désolé pour ce grand post...
Partager