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)
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
idof aMarcais not an attribute ofModelosso your query is not correct, you need to either pass an instance orMarcasor to navigate through the association if you pass theid(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 aList<Object[]>if there are multiple results), not a strong typed result unless you use theSELECT NEWconstructor expression.To summarize, the following should work:
Or, if you pass the id as the parameter of the query (but the above query is just fine):
But I would either not use projections or use a SELECT NEW constructor expression:
or (assuming
Modeloshas an appropriate constructor):And actually, I would use a named query, for example:
and use:
By the way, I would rename
idMarcaintomarcason theModelosentity,idMarcais misleading: