Search code examples
javahibernatejpacriteria

Criteria inheritance: join and search by child field


I have root entity:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "applications")
public class Application {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(cascade = CascadeType.REFRESH)
    private Customer customer;
}

which have relation to parent abstract entity:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "customers")
public abstract class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
}

And there is child entity:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class SpecificCustomer extends Customer {
    private String title;
}

I try to search for field:

@Service
@RequiredArgsConstructor
public class UserSearchRepository {

    private final EntityManager entityManager;
    private final PredicateUtils predicate;

    public void search() {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Application> criteriaQuery = criteriaBuilder.createQuery(Application.class);
        Root<Application> root = criteriaQuery.from(Application.class);
        Join<Application, Customer> applicationCustomerJoin = root.join(Application_.CUSTOMER, JoinType.LEFT);
        Predicate[] predicates = {
                predicate.like(applicationCustomerJoin.get(Customer_.NAME), "some name") // it works
                // predicate.like(applicationCustomerJoin.get(SpecificCustomer_.TITLE), "some title") // it doesn't work
                
        };
        criteriaQuery.select(root).where(predicates);
        TypedQuery<Application> query = entityManager.createQuery(criteriaQuery);
        List<Application> result = query.getResultList();

        System.out.println("test:");
        System.out.println(result);
    }

    @PostConstruct
    void init() {
        this.search();
    }
}

When I search by field that exist in parent it works:

predicate.like(applicationCustomerJoin.get(Customer_.NAME), "some name")

but when I search by field from child it doesn't work:

predicate.like(applicationCustomerJoin.get(SpecificCustomer_.TITLE), "some title")

Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSearchRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [title] on this ManagedType [my.Customer]
...
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [title] on this ManagedType [my.Customer]
    at my.UserSearchRepository.search(UserSearchRepository.java:32) ~[classes/:na]

I can't change design of model.


Solution

  • I found that I can use method treat on first join and create second join with specific type:

    Join<Application, Customer> applicationCustomerJoin = root.join(Application_.CUSTOMER, JoinType.LEFT);
    Join<Application, SpecificCustomer> applicationSpecificCustomerJoin = criteriaBuilder.treat(applicationCustomerJoin, SpecificCustomer.class);
    Predicate[] predicates = {
            predicate.like(applicationSpecificCustomerJoin.get(SpecificCustomer_.TITLE), "some title")
    };