这是我的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 + "";
}
}
从Modelosfacade
访问的方法public List<Modelos> findByMarcas(Marcas idMarca){
return em.createQuery("SELECT id, descripcion FROM Modelos WHERE idMarca = "+idMarca.getId()+"").getResultList();
}
来自控制器的调用方法
public String createByMarcas() {
//recreateModel();
items = new ListDataModel(ejbFacade.findByMarcas(current.getIdMarca()));
updateCurrentItem();
System.out.println(current.getIdMarca());
return "List";
}
我不明白为什么我会继续陷入EJB异常。
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)
答案 0 :(得分:1)
实际上,我几乎可以肯定服务器日志中有更多有用的跟踪。但无论如何,我有一些评论:
首先,id
的{{1}}不是Marca
的属性,因此您的查询不正确,您需要传递实例或Modelos
或者如果您传递Marcas
(在使用JPA时需要考虑对象和关联),则浏览关联。
其次,您执行查询的方式不正确,您应该对其进行参数化。
第三,使用SQL投影时,如果有多个结果,则会得到id
(或Object[]
),除非使用List<Object[]>
构造函数表达式,否则不会得到强类型结果。
总而言之,以下内容应该有效:
SELECT NEW
或者,如果您将id作为查询的参数传递(但上面的查询很好):
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();
}
但我不会使用投影或使用SELECT NEW构造函数表达式:
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();
}
或(假设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();
}
有一个合适的构造函数):
Modelos
实际上,我会使用命名查询,例如:
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();
}
并使用:
@NamedQuery(
name="Modelos.findByMarcas",
query="SELECT m FROM Modelos m WHERE m.idMarca = :marcas"
)
顺便说一句,我会将public List<Modelos> findByMarcas(Marcas marcas){
Query q = em.createNamedQuery("Modelos.findByMarcas");
q.setParameter("marcas", marcas);
return q.getResultList();
}
重命名为idMarca
marcas
实体,Modelos
会产生误导:
idMarca