JPA查询不返回所有值

时间:2015-07-30 14:29:02

标签: sql entity-framework jpa

我有一张表,其中包含以下值: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查询来返回它。

@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;

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

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

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

但是当我跑步时:

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

它给了我一个价值'千里马'这不在上一个列表中。

有人有什么问题吗?

1 个答案:

答案 0 :(得分:2)

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

您必须使用具有唯一主键列的表模型,并将该列映射到具有@Id注释的属性。或者,如果您想拥有一个由idproduct组成的主键,则必须使用@EmbeddedId@IdClass