1

I need to make a join using criteria, in entity mapped "one to one"

example:

public class Item {

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "transcation_id")
    private Transaction transaction;

    private String status;
    ...
    ...

}
public class Transaction {

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

}

my query is based on the entity "Transaction", however I want to get the attribute "status" of Entity "Item"

I need to do something like this in my method:

     predicates.add(root.join("?????").get("????").in("STATUS_OK"));

1 Answers1

1

I'm trying to learn criteria API right now too, so this is just a wild guess:

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<String> searchQuery = criteriaBuilder.createQuery(String.class);
Root<Transaction> root = searchQuery.from(Transaction.class);
Join<Transaction, Item> itemJoin = itemJoin.join(statusList);
List<Predicate> restrictions = new ArrayList<>();
restrictions.add(criteriaBuilder.equal(itemJoin.get(status), root.get(???)));
searchQuery.select(status).where( restrictions.toArray(new Predicate[0] ));
TypedQuery<Status> query = em.createQuery(searchQuery);
List<Status> result = query.getResultList();

the statusList needs to be created in your Item.class with something like this?:

@OneToMany(mappedBy = "status")
List<status> statusList;

May also take a look here: JOIN with CriteriaQuery?

Boommeister
  • 1,591
  • 2
  • 15
  • 54