JPA TypedQuery在给定条件下不返回结果

时间:2019-12-27 09:34:55

标签: java database jpa java-ee

我有以下型号:

    @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查询,它运行良好(仅返回英语产品)。我该怎么解决?

0 个答案:

没有答案