【发布时间】:2015-11-11 07:01:34
【问题描述】:
我正在尝试获取所有状态为 (INT) 1、2、3、4 的 Dna 实体,但 Eclipselink 给了我错误。
ReadAllQuery(name="Dna.findAllButDeleted" referenceClass=Dna jpql="SELECT r FROM Dna r where r.status =1 OR r.status=2 OR r.status=3 OR r.status=4")
@GET
@Path("all")
@Produces({"application/xml", "application/json"})
public List<Dna> findAllButDeleted(){
System.out.println("**********findAllButDeleted*************");
Query query = em.createNamedQuery("Dna.findAllButDeleted");
List<Dna> lista = query.getResultList();
return lista;
}
@NamedQuery(name = "Dna.findAllButDeleted", query = "SELECT r FROM Dna r where r.status =1 OR r.status=2 OR r.status=3 OR r.status=4"),
错误
异常 [EclipseLink-6078] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException 异常描述:对象比较的参数类不正确。 表达: [ 基础com.pako.entity.Dna] 映射:[org.eclipse.persistence.mappings.ManyToOneMapping[status]] 论据:[1]
编辑: 我的错误,现在我看到了问题。在 Dna 中,实体状态不是整数而是对象。如何使用多个参数?好像只用了最后一个。
@ManyToOne(cascade = CascadeType.MERGE)
private Dnastatus status;
Dnastatus oneStatus = new Dnastatus(new Integer(1));
Dnastatus twoStatus = new Dnastatus(new Integer(2));
Dnastatus threeStatus = new Dnastatus(new Integer(3));
Query query = em.createNamedQuery("Dna.findAllButDeleted").
setParameter("status", oneStatus).
setParameter("status", twoStatus).
setParameter("status", threeStatus);
@NamedQuery(name = "Dna.findAllButDeleted", query = "SELECT r FROM Dna r where r.status = :status"),
编辑2: 解决方案
List statusList = new ArrayList();
statusList.add(oneStatus);
statusList.add(twoStatus);
statusList.add(threeStatus);
statusList.add(fourStatus);
Query query = em.createNamedQuery("Dna.findAllButDeleted").
setParameter("statusList", statusList);
@NamedQuery(name = "Dna.findAllButDeleted", query = "SELECT r FROM Dna r where r.status IN (:statusList)")
【问题讨论】:
-
显示
Dna实体。 -
你没有使用像
@NamedQuery(name = "Dna.findAllButDeleted", query = "SELECT r FROM Dna r where r.status In (1,2,3,4)"),这样的查询 -
显示“Dna”类的“状态”字段是任何评论的先决条件。 EclipseLink 显然认为它是一个 N-1 关系字段,因此不能是整数。那么为什么不显示呢?
-
我的错误,status 是一个对象,而不是在 DB 中的任何其他地方。
-
为什么要设置参数“status”3次?如果您这样做,将使用最终值。将 List 作为单个参数会更有意义,并且该列表包含 3 个 Dnastatus 类型的值,并使用“IN”
标签: java mysql hibernate jpa eclipselink