2

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? enter image description here

7
  • You statement should be cb.gt(root.get("cc_max"), quoteDTO.getCubicCapacity())? Commented Oct 13, 2020 at 4:45
  • @samabcde I think so, I think the .gt() needs to compare what is in the database with the second parameter which in my case is an integer coming from the DTO. I think I am trying to read the cc_max incorrectly, but do not know why Commented Oct 13, 2020 at 9:19
  • Should you use mapped property cubicCapacityMax instead of column name cc_max as 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. Commented Oct 13, 2020 at 9:30
  • You must use the property name, not the db field name: root.get("cubicCapacityMax") instead of root.get("cc_max"). Try to follow a "Hello World" tutorial and get it work, before posting a question Commented Oct 13, 2020 at 9:45
  • thank you both, I will edit my question accordingly, but as I said I tried using both cubicCapacityMax and cc_max and I got the same from IntelliJ Commented Oct 13, 2020 at 11:13

1 Answer 1

2

You get the error because using Path#get(String) is not type-safe. Use Metamodel expressions if you want the compiler to correctly interpret the type you want to be returned. If you use string parameter, you need to specify the type like that:

Expression<Integer> expr = root.<Integer> get("cubicCapacityMax");

Using Metamodel, the same expression could be obtained in this way:

Expression<Integer> expr = root.get(BaseQuote_.cubicCapacityMax);

Also note that the string parameter must correspond to the name of a property (cubicCapacityMax in this case) and not to the db name of the field (cc_max).

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.