原生查询(JPA)未重置并返回相同的旧结果 [英] Native Query (JPA ) not reset and return the same old result

查看:204
本文介绍了原生查询(JPA)未重置并返回相同的旧结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本机sql查询,如下所示:

I have a native sql query as the following :

for(init i=0; i <=2 ; i++){ 
String sql = "Select * from accounts where id = ?"; 
Query query = em.createNativeQuery(sql,AccountBean.class); 
query.setParameter(1, i ); 

AccountBean accountBean = (AccountBean)query.getSingleResult(); 
} 

第一个循环它正常工作但第一个循环后的任何循环返回与第一个相同的结果,我调试它,参数改变,如果我改变它的工作正确

for the first loop it works correct but any loop after the first one return the same result to the first one , i debug it, the parameter changed , it works correct if i changed

Query query = em.createNativeQuery(sql,AccountBean.class); 

Query query = em.createNativeQuery(queryString); 

问候
Wish79

Regards Wish79

推荐答案

每个JPA实体都必须有一个主键。您的JPA实体可能无法正确反映数据库表上的主键(如果有)。

Every JPA entity must have a primary key. Your JPA entities may not properly reflect the primary key, if any, on the database table.

我遇到了同样的问题。在我的模型类中,我只有一个用@Id注释的类变量。但是,这并不是表本身的准确反映,表本身具有复合主键。因此,我的查询结果返回了正确的行数,但每个行都包含相同的值,即使db中的实际数据不同。例如,此查询:

I ran into the same problem. In my model class I had only one class variable annotated with @Id. However, that was not an accurate reflection of the table itself, which has a composite primary key. Thus, my query results returned the correct number of rows, but each confoundingly contained the same values, even though the actual data was different in the db. For example, this query:

Query query = entityManager.createQuery
("SELECT tbl FROM Tbl tbl WHERE tbl.id = 100 
  and tbl.code in ('A','B','C')");

...返回10行,每行显示'A>代码的'A 。但实际上,这10行中有9行具有不同的代码值('B'或'C')。似乎结果被缓存和/或忽略了 tbl.code 谓词。 (无论我使用的是JPQL 还是 Native SQL。)非常令人困惑。

...returned 10 rows, each showing a code of 'A'. But in actuality 9 of those 10 rows had a different code value ('B' or 'C'). It seemed as if the results were being cached and/or the tbl.code predicate was ignored. (That happened whether I used JPQL or Native SQL.) Very confusing.

为了解决这个问题,我在模型中添加了一个额外的@Id注释反映复合主键:

To fix this I added an additional @Id annotation to my model to reflect the composite primary key:

@Id
@Column(name = "Code")
public String getCode() {
    return this.code;
}

现在查询正确返回数据和代码选择标准不再被有效忽略。

Now the query returns the data correctly and the code select criteria is no longer effectively ignored.

编辑:虽然以上对我有用,但在进一步研究中,似乎是一种更好的方法来配置单独的JPA实体复合主键类。请参见 http://docs.oracle.com/cd/E16439_01/ doc.1013 / e13981 / cmp30cfg001.htm

Although the above worked for me, on further research it seems a better approach to configure a separate JPA Entity composite primary key class. See http://docs.oracle.com/cd/E16439_01/doc.1013/e13981/cmp30cfg001.htm.

例如,这是一个带有嵌入式主键的Entity类(请参阅@EmbeddedId):

For example, here's an Entity class with an embedded primary key (see @EmbeddedId):

/**
 * The persistent class for the SOME_TABLE database table.
 */
@Entity
@Table(name = "SOME_TABLE")
public class SomeTable implements Serializable {

    @EmbeddedId
    private SomeTablePk   id;

    @Column(name = "NUMBER_HRS")
    private BigDecimal        numberHrs; 
    ...

...这里是复合主键类(参见@Embeddable ):

...and here's the composite primary key class (see @Embeddable):

@Embeddable
public class SomeTablePk implements Serializable {

    @Column(name = "SOME_ID")
    private String     someId;

    @Column(name = "ANOTHER_ID")
    private BigDecimal anotherId;

    public String getSomeId() {
        return someId;
    }
    ...

这篇关于原生查询(JPA)未重置并返回相同的旧结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆