I have a table with the following columns and the corresponding POJO class
@Entity
@Table(name = "base_quote")
public class BaseQuote {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "cc_min")
private int cubicCapacityMin;
@Column(name = "cc_max")
private int cubicCapacityMax;
@Column(name = "car_age_min")
private int carAgeMin;
@Column(name = "car_age_max")
private int carAgeMax;
@Column(name = "base_amount")
public double baseAmount;
constructor, getters and setters
I am trying to write a query that will return the baseAmount for a car, the cubic capacity of which is between cc_min and cc_max and age is between carAgeMin and carAgeMax. I am trying to get either the entire row so I can later use the baseAmount of that row.
I am receiving the cc and age of the car trough a DTO.
private double baseAmount (QuoteDTO quoteDTO) {
try (Session session = sessionFactory.openSession()) {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<BaseQuote> cq = cb.createQuery(BaseQuote.class);
Root<BaseQuote> root = cq.from(BaseQuote.class);
cq.select(root).where(cb.gt(root.get("cc_max")), quoteDTO.getCubicCapacity());
Query<BaseQuote> query = session.createQuery(cq);
List<BaseQuote> results = query.getResultList();
}
return null;
}
I am getting something wrong because I get cannot resolve method for cb.gt(root.get("cc_max") I tried also using the class field name cubicCapacityMax instead of cc_max, but I an getting the same error. I want to get the rows in which the cc_max is greater than the DTO cubic capacity, so I used the .gt() as proposed in the demo I was using. It underlines everything in the brackets of the .gt(). I know that this does not produce the specific result I am actually looking for, but it is the first part of the expression. Where am I wrong?

cb.gt(root.get("cc_max"), quoteDTO.getCubicCapacity())?cubicCapacityMaxinstead of column namecc_maxas you are not writing sql? I suggest you to provide details of your new problem in another question, as the current question is just due to a simple typo.root.get("cubicCapacityMax")instead ofroot.get("cc_max"). Try to follow a "Hello World" tutorial and get it work, before posting a question