Hibernation and arithmetic associated with certain series

I use Hibernate and you need to perform a basic arithmetic function for the results. Here is my situation:

I save odometerand fuelwould like to calculate fuelEconomy. But for this I need to know the previous reading odometer, which will be obtained from the previous result. Here is an illustration.

Table:

ODOMETER | FUEL
65000.0  | 5.000
65500.0  | 15.000

POJO:

public class FuelLog {
  private double odometer;
  private double fuel;
  private double fuelEconomy;

  /* Getters and Setters */
}

Can I do this using criteria or do I need to resort to an HQL query? Or would it be better to leave on another layer together?

+3
source share
1 answer

I would do it differently. I would save the start odometer value in the database, and I would save how many KM (or miles) were completed:

START |  TRIP | FUEL
60000 |  5000 | 5.0
65000 | 15000 | 15.0

Then you will have this as your Java class:

public class FuelLog {
  private double start;
  private double trip;
  private double fuel;
}

, , . , , "" + , / .

, .

, , , , , " " . :

START |  TRIP | FUEL | INDEX
60000 |  5000 | 5.0  | 1
65000 | 15000 | 15.0 | 2

Java, FuelLog, Hibernate:

public class Car {
  private List<FuelLog> fuelLog;
  public double getFuelEconomy(FuelLog log){/* your implementation goes here */}
}

Car , . fuelEconomy FuelLog.

+1

Source: https://habr.com/ru/post/1784722/


All Articles