简体   繁体   English

JPA 标准 API 加入

[英]JPA Criteria API join

Help me plz with one moment.请帮我一下。 I read about 10 articles already, but don't understand join moment.我已经阅读了大约 10 篇文章,但不了解加入时刻。 I have 2 tables:我有2张桌子:

public class News implements Serializable {
@Id
@GeneratedValue (generator = "increment")
@GenericGenerator (name = "increment", strategy = "increment")
private int id;
@Column
private String name;
@Column
private Date created;
@Column
private String data;
@ManyToOne (cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn (name = "category_id")
private NewsCategoryDict category;
// getters, setters
}

and

public class NewsCategoryDict implements Serializable {
@Id
@GeneratedValue (generator = "increment")
@GenericGenerator (name = "increment", strategy = "increment")
private int id;
@Column
private String name;
@OneToMany (mappedBy = "category", cascade = CascadeType.ALL)
private List<News> news = new ArrayList<>();
}

I want a query works like我想要一个查询像

SELECT * FROM news, categorynews WHERE news.category_id = categorynews.id;

And then get the result in jsp with然后在jsp中得到结果

    <div id="list_news">
        <c:forEach items="${news}" var="news">
            <h5>${news.id} : ${news.name} - ${news.created} ; ${news.data} (${news.category.name})</h5>
        </c:forEach>
    </div>

And I just can't understand this JOIN with Criteria API.我就是无法理解这个 JOIN with Criteria API。 Can you help me ?你能帮助我吗 ? Try to use this snippet, but get a error尝试使用此代码段,但出现错误

public List<News> getAll() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<News> cq = cb.createQuery(News.class);
    Root<News> rootFromNews = cq.from(News.class);
    Join<NewsCategoryDict, News> join = rootFromNews.join("category");
    cq.select(join);

    return em.createQuery(cq).getResultList();
}

PropertyNotFoundException: Property 'created' not found on type ru.r1k0.spring.model.NewsCategoryDict PropertyNotFoundException:在 ru.r1k0.spring.model.NewsCategoryDict 类型上找不到属性“创建”

Assuming you want to return an instance of News associated to an instance of NewsCategoryDict , your criteria query should look as follows:假设您要返回与NewsCategoryDict实例关联的News实例,您的条件查询应如下所示:

public List<News> getAll() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<News> cq = cb.createQuery(News.class);
    Root<News> rootFromNews = cq.from(News.class);
    Join<News, NewsCategoryDict> join = rootFromNews.join("category"); // #1
    cq.select(rootFromNews);  // #2

    return em.createQuery(cq).getResultList();
}

The modified lines are marked with #1 and #2 comments.修改后的行标有#1#2注释。

The query should return all News which have a matching NewsCategoryDict ;查询应返回所有具有匹配NewsCategoryDict News but News records which are not associated to a NewsCategoryDict record will not be returned.但不会返回与NewsCategoryDict记录无关的News记录。

Your error has nothing to do with the join!您的错误与加入无关! What is actually happening is that in the JSP fragment you are trying to access ${news.created} which does not exist in the NewsCategoryDict .实际发生的是,在 JSP 片段中,您试图访问${news.created} ,而${news.created}NewsCategoryDict中不存在。 I believe the error is in the JSP fragment, not in the Criteria query.我相信错误是在 JSP 片段中,而不是在 Criteria 查询中。

The way I understand it is that you want to list the News object, but in the query you are selecting the NewsCategoryDict and this is why at the end you end up with missing attribute because the NewsCategoryDict does not contain ${news.created}我的理解是你想列出 News 对象,但在查询中你选择的是 NewsCategoryDict 这就是为什么最后你最终会丢失属性,因为 NewsCategoryDict 不包含${news.created}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM