I have a relation between 3 Entities (Invoice and InvoiceRating adn User)
I’m not able to produce a invoice list sorted by rating for a specific user. Even if there is no rating, the invoice should be listed (user is just a parameter to filter rating)
{
invoiceA :
rating:2
others fields
invoiceB :
rating:3
others fields
invoiceC :
others fields
}
@Entity
public class Invoice {
@OneToMany(mappedBy = "invoice", fetch = FetchType.LAZY)
Set<InvoiceRating> ratings;
}
@Entity
public class InvoiceRating {
@EmbeddedId
BusinessTransactionRating ratingKey;
@ManyToOne
@MapsId("userId")
@JoinColumn(name = "user_id")
User user;
@ManyToOne
@MapsId("transactionId")
@JoinColumn(name = "invoice_id")
Invoice invoice;
int rating;
}
@Embeddable
public class BusinessTransactionRating {
@Column(name = "transaction_id")
Long transactionId;
@Column(name = "user_id")
Long userId;
}
@Entity
public class User {
}
The code that works :
CriteriaQuery<Invoice> criteriaQuery = cBuilder.createQuery(Invoice.class);
Root<Invoice> rootInvoice = criteriaQuery.from(Invoice.class);
pageable.getSort().stream().forEach(order -> { // sorting
Path<Object> prop = rootInvoice.get(order.getProperty());
list.add(order.isAscending() ? cBuilder.asc(prop) : cBuilder.desc(prop));
// finalQuery.orderBy();
});
CriteriaQuery<Invoice> criteriaQueryOrder = finalQuery.orderBy(list);
// TO DO / Add the count Line and the rebuild pageImpl
TypedQuery<Invoice> queryPaginate = entityManager.createQuery(criteriaQueryOrder);
What I have to do to add a select of InvoiceRating that are filtered by a User?
New contributor
Thomas Mancini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6