Java JPA的query.getResultList()方法如何改写为长尾?

2026-04-02 14:301阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计310个文字,预计阅读时间需要2分钟。

Java JPA的query.getResultList()方法如何改写为长尾?

我使用JPA 1.0进行查询:Query query=em.createNamedQuery(getThresholdParameters); query.setParameter(1, Integer.parseInt(circleId)); List结果

我使用JPA1.0:Queryquery;queryem.createNamedQuery(getThresholdParameters);query.setParameter

我使用JPA 1.0:

Query query; query = em.createNamedQuery("getThresholdParameters"); query.setParameter(1, Integer.parseInt(circleId)); List resultList = new ArrayList(); resultList = query.getResultList();

这里我得到了List的结果,因此我必须键入将该行的所有参数转换为它们各自的类型,这很麻烦.

在JPA 2.0中,有TypedQuery返回类型为1的实体对象指定.

但是当我使用JPA 1时,我无法使用它.

如何获得结果作为我想要的类型的实体对象?

编辑:QUERY

Java JPA的query.getResultList()方法如何改写为长尾?

@Entity@Table(name="GMA_THRESHOLD_PARAMETERS")@NamedQuery( name = "getThresholdParameters", query = "select gmaTh.minNumberOc, gmaTh.minDurationOc, gmaTh.maxNumberIc, gmaTh.maxDurationIc, gmaTh.maxNumberCellId," + "gmaTh.distinctBnumberRatio, gmaTh.minPercentDistinctBnumber from GmaThresholdParameter gmaTh " + "where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3 " )

解决方法:

您的查询选择了许多字段.这样的查询总是返回Object数组的列表.如果您想要一个包含GmaThresholdParameter实体实例的列表,那么查询应该是

select gmaTh from GmaThresholdParameter gmaTh where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3

然后,获取实体列表的代码将是

List resultList = query.getResultList();

您将从编译器获得类型安全警告,您可以忽略.

本文共计310个文字,预计阅读时间需要2分钟。

Java JPA的query.getResultList()方法如何改写为长尾?

我使用JPA 1.0进行查询:Query query=em.createNamedQuery(getThresholdParameters); query.setParameter(1, Integer.parseInt(circleId)); List结果

我使用JPA1.0:Queryquery;queryem.createNamedQuery(getThresholdParameters);query.setParameter

我使用JPA 1.0:

Query query; query = em.createNamedQuery("getThresholdParameters"); query.setParameter(1, Integer.parseInt(circleId)); List resultList = new ArrayList(); resultList = query.getResultList();

这里我得到了List的结果,因此我必须键入将该行的所有参数转换为它们各自的类型,这很麻烦.

在JPA 2.0中,有TypedQuery返回类型为1的实体对象指定.

但是当我使用JPA 1时,我无法使用它.

如何获得结果作为我想要的类型的实体对象?

编辑:QUERY

Java JPA的query.getResultList()方法如何改写为长尾?

@Entity@Table(name="GMA_THRESHOLD_PARAMETERS")@NamedQuery( name = "getThresholdParameters", query = "select gmaTh.minNumberOc, gmaTh.minDurationOc, gmaTh.maxNumberIc, gmaTh.maxDurationIc, gmaTh.maxNumberCellId," + "gmaTh.distinctBnumberRatio, gmaTh.minPercentDistinctBnumber from GmaThresholdParameter gmaTh " + "where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3 " )

解决方法:

您的查询选择了许多字段.这样的查询总是返回Object数组的列表.如果您想要一个包含GmaThresholdParameter实体实例的列表,那么查询应该是

select gmaTh from GmaThresholdParameter gmaTh where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3

然后,获取实体列表的代码将是

List resultList = query.getResultList();

您将从编译器获得类型安全警告,您可以忽略.