JPA查询并非返回所有值 [英] JPA query returns not all values

查看:175
本文介绍了JPA查询并非返回所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下值的表:id,产品,productid,品牌,描述,总和,数量.

I have a table which has these values: id, product, productid, brand, description, sum, quantity.

7, December, 74909, Fuji, Good bicycle, 2000, 4
7, March, 74534, Giant, Good goof bicycle, 2000, 4
8, Winter, 83458, Ghost, Very Good bicycle, 5000, 2
8, Spring, 42144, Regis, Good bicycle, 92000, 7
8, Summer, 47122, Maxima, Good veryyy bicycle, 92000, 7
9, Okutava, 53681, Sunny, Good bicycle, 9000, 5
10, Pavilion, 94847, Eclipse, Good bicycle, 92000, 7
10, Cicso, 47465, Omega, Good bicycle, 92000, 7
11, Lenovo, 16611, Hemi Cuda, Good bicycle, 92000, 7
11, Coral, 14342, Mustang, Bad bicycle, 92000, 7
12, Duple, 22222, Pulsar, Normal bicycle, 92000, 7
12, Shimano, 52515, Skyline, Good bicycle, 92000, 7
13, Salaror, 14542, Silvia, Good bicycle, 92000, 7
14, Phone, 21876, Miata, Good bicycle, 92000, 7
14, Imago, 12111, Titan, Good bicycle, 92000, 7
15, Kubas, 43777, Celica, Good bicycle, 92000, 7
16, Iki, 65427, Civic, Good bicycle, 92000, 7
16, Maxima, 13999, Accord, Good bicycle, 92000, 7

我正在使用JPA查询返回它.

I am using JPA query to return it.

@Entity
@Table(name="ITEMS")
@NamedQuery(name="returnItems",
            query="SELECT c FROM Items c WHERE c.id > 0")

    public class Items implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;
    private String product;
    private Integer productId;
    private String brand;
    private String description;
    private Integer sum;
    private Integer quantity;

但是当我运行它时,我得到了错误的值:

But when I run it, I get wrong values:

7 December 74909 2000
7 December 74909 2000
8 Winter 83458 5000
8 Winter 83458 5000
8 Winter 83458 5000
9 Okutava 53681 9000
10 Pavilion 94847 92000
10 Pavilion 94847 92000
11 Lenovo 16611 92000
11 Lenovo 16611 92000
12 Duple 22222 92000
12 Duple 22222 92000
13 Salaror 14542 92000
14 Phone 21876 92000
14 Phone 21876 92000
15 Kubas 43777 92000
16 Iki 65427 92000
16 Iki 65427 92000

问题是,当我运行查询时,如您所见,它不会打印数据库中的所有值.

The problem is when I run query it do not print all values from database as you can see.

但是当我跑步时:

@NamedQuery(name="returnItems",
        query="SELECT c FROM Items c WHERE c.product = 'Maxima'")

它给了我一个'Maxima'值,不在上一个列表中.

It gives me a value 'Maxima' which is not in the previous list.

有人有问题吗?

推荐答案

您正在调用的列id不是真实的ID-它不是主键(对于所有行都是唯一的).而且由于JPA提供程序正在为ID缓存实体,所以它为具有相同ID的所有行返回相同的实体.

The column that you are calling id is not a real id - it is not a primary key (which is unique for all rows). And because the JPA provider is caching the entities for an id, it returns the same entity for all rows with the same id.

您必须使用一个表模型,其中具有唯一的主键列,并将该列映射到带有@Id批注的属性.或者,如果您想要具有例如idproduct组成的主键,则必须使用@EmbeddedId@IdClass.

You have to use a table model where you have a unique primary key column and map that column to the attribute with the @Id annotation. Or if you want to have a primary key that is a composition of, for example, id and product, than you will have to use either an @EmbeddedId or @IdClass.

这篇关于JPA查询并非返回所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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