N
N
Natalia Tsarkevich2014-02-05 17:18:51
Java
Natalia Tsarkevich, 2014-02-05 17:18:51

I can't figure out the façade. What am I missing?

Good day. Help is urgently needed. There is a facade, and a controller that works with it, but when you try to create / edit / delete NullPointerException - that is, the facade (object) is empty for some reason.
Abstract facade

package entity;

import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Nafanya Victorovna
 */
public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(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();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
}

facade directly which I am trying to do something with
package entity;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Nafanya Victorovna
 */
@Stateless
public class UsermyFacade extends AbstractFacade<Usermy> {
    @PersistenceContext(unitName = "MyPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsermyFacade() {
        super(Usermy.class);
    }
    
}

an entity associated with a table in the database
package entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Nafanya Victorovna
 */
@Entity
@Table(name = "usermy")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Usermy.findAll", query = "SELECT u FROM Usermy u"),
    @NamedQuery(name = "Usermy.findByIduser", query = "SELECT u FROM Usermy u WHERE u.iduser = :iduser"),
    @NamedQuery(name = "Usermy.findByIsAuth", query = "SELECT u FROM Usermy u WHERE u.isAuth = :isAuth")})
public class Usermy implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "iduser")
    private String iduser;
    @Column(name = "isAuth")
    private Boolean isAuth;

    public Usermy() {
    }

    public Usermy(String iduser) {
        this.iduser = iduser;
    }

    public String getIduser() {
        return iduser;
    }

    public void setIduser(String iduser) {
        this.iduser = iduser;
    }

    public Boolean getIsAuth() {
        return isAuth;
    }

    public void setIsAuth(Boolean isAuth) {
        this.isAuth = isAuth;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (iduser != null ? iduser.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Usermy)) {
            return false;
        }
        Usermy other = (Usermy) object;
        if ((this.iduser == null && other.iduser != null) || (this.iduser != null && !this.iduser.equals(other.iduser))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Usermy[ iduser=" + iduser + " ]";
    }
    
}

my wonderfully non-working controller
package entity;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;


@Named("usermyController")
@SessionScoped
public class UsermyController implements Serializable {

    private Usermy user;
    @EJB
    private UsermyFacade ejbFacade;

    public UsermyController(){}

    private UsermyFacade getFacade(){
        return ejbFacade;
    }

    public void create(){
            this.user = new Usermy();
            String id = OAuth.getId();
            boolean bool = OAuth.getAccess();
            this.user.setIduser(id);
            this.user.setIsAuth(bool);  
            System.out.println(this.user.getIduser());
            getFacade().create(this.user);
    }

    public void update(String id) {
            this.user = getUsermy(id);
            boolean bool = OAuth.getAccess();
            this.user.setIsAuth(bool);
            getFacade().edit(this.user);
   }
        
    public void delete(String id) {
            this.user = getUsermy(id);
            this.user.setIsAuth(Boolean.FALSE);
    }
    
    public Usermy getUsermy(java.lang.String id) {
        return ejbFacade.find(id);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Lopatin, 2014-02-05
@NafanyaVictorovna

1. It doesn't even compile. You have a AbstractFacadeone-argument UsermyFacadeconstructor and a default constructor.
2. Everything breaks because you build UsermyControllermanually by calling new UsermyController(). Naturally, no dependencies are injected into it. UsermyControllermust be constructed by the container. It can be received by a servlet as a field with an annotation @Injecteg.

N
Natalya Tsarkevich, 2014-02-06
@NafanyaVictorovna

+ I added the converter, apparently it was missing there, because after that everything worked fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question