I am learning Entity Bean and I get this error when I am doing my lab. I chose to create a Session Bean for Entity class (for Entity Product). This file is automatically created by NetBeans. But it informs that ProductsFacade.java uses unchecked or unsafe operations.
Here is the code:
@Stateless
public class ProductsFacade implements ProductsFacadeRemote {
@PersistenceContext(unitName = "NhungBHSE02082_SE0606_AdvJava_Lab10_11-ejbPU")
private EntityManager em;
public void create(Products products) {
em.persist(products);
}
public void edit(Products products) {
em.merge(products);`enter code here`
}
public void remove(Products products) {
em.remove(em.merge(products));
}
public Products find(Object id) {
return em.find(Products.class, id);
}
public List<Products> findAll() {
return em.createQuery("select object(o) from Products as o").getResultList();
}
public List<Products> findRange(int[] range) {
Query q = em.createQuery("select object(o) from Products as o");
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
return ((Long) em.createQuery("select count(o) from Products as o").getSingleResult()).intValue();
}
}
the warning is due to the following methods:
What happens internally is
query.getResultList()returns genericList. Each Object in the List is of typeProducts. You know that, but compiler doesn’t know. The type can be determined only at runtime. So the compiler shows the warning uncheck or unsafe operation. You can safely ignore this warning.If you are really worried, then you can add the annotation
@SuppressWarnings("unchecked")to these two methods for the warning to disappear.