0

我有以下情况 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 有一个单独的类,但到目前为止我还没有找到正确实现它的方法。

有任何想法吗?

4

1 回答 1

0

根据克里斯的评论,我提供了列名而不是实体类中的关系名称(在实体类中标记为映射)

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

现在一切都按预期工作!

于 2013-04-17T08:03:14.597 回答