views:

32

answers:

1

Here my EJB

@Entity
@Table(name = "modelos")
@NamedQueries({
    @NamedQuery(name = "Modelos.findAll", query = "SELECT m FROM Modelos m"),
    @NamedQuery(name = "Modelos.findById", query = "SELECT m FROM Modelos m WHERE m.id = :id"),
    @NamedQuery(name = "Modelos.findByDescripcion", query = "SELECT m FROM Modelos m WHERE m.descripcion = :descripcion")})
public class Modelos implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "descripcion")
    private String descripcion;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idModelo")
    private Collection<Produtos> produtosCollection;
    @JoinColumn(name = "id_marca", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Marcas idMarca;

    public Modelos() {
    }

    public Modelos(Integer id) {
        this.id = id;
    }

    public Modelos(Integer id, String descripcion) {
        this.id = id;
        this.descripcion = descripcion;
    }

    public Modelos(Integer id, Marcas idMarca) {
        this.id = id;
        this.idMarca = idMarca;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public Collection<Produtos> getProdutosCollection() {
        return produtosCollection;
    }

    public void setProdutosCollection(Collection<Produtos> produtosCollection) {
        this.produtosCollection = produtosCollection;
    }

    public Marcas getIdMarca() {
        return idMarca;
    }

    public void setIdMarca(Marcas idMarca) {
        this.idMarca = idMarca;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 Modelos)) {
            return false;
        }
        Modelos other = (Modelos) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "" + descripcion + "";
    }

}

And the method accesing from the Modelosfacade

public List<Modelos> findByMarcas(Marcas idMarca){
    return em.createQuery("SELECT id, descripcion FROM Modelos WHERE idMarca = "+idMarca.getId()+"").getResultList();
}

And the calling method from the controller

public String createByMarcas() {
    //recreateModel();
    items = new ListDataModel(ejbFacade.findByMarcas(current.getIdMarca()));
    updateCurrentItem();
    System.out.println(current.getIdMarca());
    return "List";
}

I do not understand why I keep falling in an EJB exception.

Caused by: javax.ejb.EJBException
 at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5070)
 at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:4968)
 at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4756)
 at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)
 at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1906)
 at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:198)
 at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:84)
 at $Proxy347.findByMarcas(Unknown Source)
 at controladores.__EJB31_Generated__ModelosFacade__Intf____Bean__.findByMarcas(Unknown Source)
+1  A: 

Actually, I'm almost sure that there are more useful traces in the server logs. But anyway, I have some remarks:

First of all, the id of a Marca is not an attribute of Modelos so your query is not correct, you need to either pass an instance or Marcas or to navigate through the association if you pass the id (you need to think object and associations when using JPA).

Secondly, the way you are performing your query is not correct, you should parametrize it.

Thirdly, when using SQL projections, you get an Object[] (or a List<Object[]> if there are multiple results), not a strong typed result unless you use the SELECT NEW constructor expression.

To summarize, the following should work:

public List<Object[]> findByMarcas(Marcas marcas){
    Query q = em.createQuery("SELECT m.id, m.descripcion FROM Modelos m WHERE m.idMarca = :marcas");
    q.setParameter("marcas", marcas);
    return q.getResultList();
}

Or, if you pass the id as the parameter of the query (but the above query is just fine):

public List<Object[]> findByMarcas(Marcas marcas){
    Query q = em.createQuery("SELECT m.id, m.descripcion FROM Modelos m WHERE m.idMarca.id = :idMarca");
    q.setParameter("idMarca", marcas.getId());
    return q.getResultList();
}

But I would either not use projections or use a SELECT NEW constructor expression:

public List<Modelos> findByMarcas(Marcas marcas){
    Query q = em.createQuery("SELECT m FROM Modelos m WHERE m.idMarca.id = :idMarca");
    q.setParameter("idMarca", marcas.getId());
    return q.getResultList();
}

or (assuming Modelos has an appropriate constructor):

public List<Modelos> findByMarcas(Marcas marcas){
    Query q = em.createQuery("SELECT NEW com.acme.Modelos(m.id, m.descripcion) FROM Modelos m WHERE m.idMarca.id = :idMarca");
    q.setParameter("idMarca", marcas.getId());
    return q.getResultList();
}

And actually, I would use a named query, for example:

@NamedQuery(
    name="Modelos.findByMarcas",
    query="SELECT m FROM Modelos m WHERE m.idMarca = :marcas"
)

and use:

public List<Modelos> findByMarcas(Marcas marcas){
    Query q = em.createNamedQuery("Modelos.findByMarcas");
    q.setParameter("marcas", marcas);
    return q.getResultList();
}

By the way, I would rename idMarca into marcas on the Modelos entity, idMarca is misleading:

@ManyToOne(optional = false)
private Marcas marcas;
Pascal Thivent
Crytal Clear! I'm really thankfull great explanation! will apply changes right now (actually as soon as argentina's match is finished) but really thank you very much again. I let you know as is working how it goes!
Ignacio
You're welcome, glad you found this helpful. And let me know if the problem is not solved.
Pascal Thivent
Problem solved thank you very much!
Ignacio