首页 > 解决方案 > JPA TypedQuery 不返回给定条件的结果

问题描述

我有以下型号:

    @Entity
    @Table(name = "product")
    public class Product implements Serializable {

        @Id
        @GeneratedValue(generator = "product_id_seq", strategy = GenerationType.SEQUENCE)
        @SequenceGenerator(name = "product_id_seq", sequenceName = "product_id_seq", allocationSize = 1)
        @Column(name = "id")
        private Long id;

        @OneToMany(mappedBy = "product")
        private List<ProductContent> productContent;

        @Column(name = "expired_at", nullable = false)
        @Temporal(javax.persistence.TemporalType.TIMESTAMP)
        private Date expiredAt;

        @Column(name = "created_at", nullable = false)
        @Temporal(javax.persistence.TemporalType.TIMESTAMP)
        private Date createdAt;
    @Entity
    @Table(name = "product_content")
    public class ProductContent {
        @Id
        @GeneratedValue(generator = "product_content_id_seq", strategy = GenerationType.SEQUENCE)
        @SequenceGenerator(name = "product_content_id_seq", sequenceName = "product_content_id_seq", allocationSize = 1)
        @Column(name = "id")
        private Long id;

        @Column(name = "title", nullable = false)
        private String title;

        @Column(name = "content", columnDefinition="TEXT", nullable = false)
        private String content;

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "product_id")
        private Product product;

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "supported_locale_id")
        private SupportedLocale locale;
    @Entity
    @Table(name = "supported_locale")
    public class SupportedLocale {
        @Id
        @GeneratedValue(generator = "supported_locale_id_seq", strategy = GenerationType.SEQUENCE)
        @SequenceGenerator(name = "supported_locale_id_seq", sequenceName = "supported_locale_id_seq", allocationSize = 1)
        @Column(name = "id")
        private Long id;

        @Column(name = "language_name", nullable = false)
        private String languageName;

        @Column(name = "language_tag", nullable = false)
        private String languageTag;

        @OneToMany(mappedBy = "locale")
        private List<ProductContent> productContent;

我想用英语获取所有产品。我正在使用以下类型的查询:

List<Product> products = em.createQuery("SELECT p FROM Product p JOIN p.productContent pc JOIN pc.locale l WHERE l.languageName=:languageName", Product.class)
                .setParameter("languageName", "English")
                .getResultList();

它返回给我的产品列表不限于英语(例如,有西班牙语 productContent)。我还尝试直接在数据库(Postgres)上使用生成的 SQL 查询,它工作正常(它只返回英语产品)。我该如何解决?

标签: javadatabasejpajakarta-ee

解决方案


推荐阅读