Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity bean with uncheck or unsafe operation

Tags:

java

ejb

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();
    }
}
like image 262
Bee Bee Avatar asked Sep 18 '25 13:09

Bee Bee


1 Answers

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.

like image 62
PC. Avatar answered Sep 21 '25 03:09

PC.