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:
public List<Products> findAll() {
return em.createQuery("select object(o) from Products as o").getResultList();
}
public List<Products> findRange(int[] range) {
...
return q.getResultList();
}
What happens internally is query.getResultList()
returns generic List
. Each Object in the List is of type Products
. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With