JPA2,LEFT JOIN,Criteria API

时间:2013-04-16 11:17:37

标签: java jpa-2.0 eclipselink

我有以下情况需要在我的条件构建器中实现SQL以进行动态查询创建。

select t1.ticketnr 
from tbl_ticket t1 
left join tbl_tickets_updates t2 on t1.ticketnr = t2.ticketnr 
where t1.description ilike '%EXAMPLE%' or t2.description ilike '%EXAMPLE%';

在我的代码中,我有以下内容:

tbl_ticket = Tickets.class PK = ticketnr

tbl_tickets_updates = TicketsUpdates.class和TicketsUpdatesPK.class PK = ticketnr,updatedby,timeofupdate

        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Tickets> query = builder.createQuery(Tickets.class);
        EntityType<Tickets> type = em.getMetamodel().entity(Tickets.class);
        Root<Tickets> root = query.from(Tickets.class);
        List<Predicate> predicatesAnd = new ArrayList<Predicate>();
...
    if (text.length() != 0) {
                    predicatesAnd.add(builder.or(
                            builder.like(
                            builder.lower(
                            root.get(
                            type.getDeclaredSingularAttribute("description", String.class))), "%" + text.toLowerCase() + "%"),
                            builder.like(
                            builder.lower(
                            root.get(
                            type.getDeclaredSingularAttribute("summary", String.class))), "%" + text.toLowerCase() + "%")));
                    Join<Tickets, TicketsUpdates> tupdates = root.join("ticketnr", JoinType.LEFT);
                    predicatesAnd.add(builder.like(builder.lower(tupdates.get("description").as(String.class)), "%" + text.toLowerCase() + "%"));
                }

我的代码在左连接时失败: java.lang.IllegalStateException:CAN_NOT_JOIN_TO_BASIC

我相信这是因为复合PK有一个单独的类,但到目前为止我还没有找到一种方法来正确实现它。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

基于Chris的评论,我提供了列名而不是实体类中的关系名(在实体类中标记为Map)

 CriteriaBuilder builder = em.getCriteriaBuilder();
            CriteriaQuery<Tickets> query = builder.createQuery(Tickets.class);
            EntityType<Tickets> type = em.getMetamodel().entity(Tickets.class);
            EntityType<TicketsUpdates> typeTU = em.getMetamodel().entity(TicketsUpdates.class);
            Root<Tickets> root = query.from(Tickets.class);
            Root<TicketsUpdates> rootTicketsUpdates = query.from(TicketsUpdates.class);

            Join<Tickets,TicketsUpdates> tupdates = rootTicketsUpdates.join("tickets");

我必须添加第二个(它丢失了)并将我的查询更改为:

if (text.length() != 0) {
                predicatesAnd.add(builder.or(
                        builder.like(
                        builder.lower(
                        root.get(
                        type.getDeclaredSingularAttribute("description", String.class))), "%" + text.toLowerCase() + "%"),
                        builder.like(
                        builder.lower(
                        root.get(
                        type.getDeclaredSingularAttribute("summary", String.class))), "%" + text.toLowerCase() + "%"),
                        builder.like(
                        builder.lower(
                        tupdates.get(
                        typeTU.getDeclaredSingularAttribute("description", String.class))), "%"+text.toLowerCase()+"%")
                        ));
            }

    query.orderBy(builder.desc(root.get("startdate")));
    query.distinct(true);

现在一切正常!