title: Adding values from another table
body: <p>Simple app with spring boot so I'm using jpa, hibernate etc.</p>
  <p>User is adding values to trip_data, two of this values are in columns shown below.</p>
  <pre><code>| ID | trip_data_begin | trip_data_end | 
  
  |  1  |     x1         |     y1        |
  
  |  2  |     x2         |     y2        |
  
  
  
  |  ID | counter_data_begin | counter_data_end |
  
  |  1  |        x1          |        y2        |
  </code></pre>
  <p>x1 - smallest value</p>
  <p>y2 - highest value</p>
  <p>What I want is to copy two of this values to counter_data. I was able to this with query but when user add another row and y3 is higher than y2 then values in counter_data are incorrect. Maybe operate only on the last copied row in counter_data?</p>
  <p>I would be very grateful for your help</p>
  <pre><code>@Entity
  @Table(name = &quot;trip_data&quot;)
  public class TripData {
      @Id
      @GeneratedValue(generator = &quot;inc&quot;)
      @GenericGenerator(name = &quot;inc&quot;, strategy = &quot;increment&quot;)
      private int id;
      private int tripDateBegin;
      private int hourBegin;
      private String townBegin;
      private String countryBegin;
      private long tripCounterBegin;
      private int tripDateEnd;
      private int hourEnd;
      private String townEnd;
      private String countryEnd;
      private long tripCounterEnd;
  
      public TripData() {
      }
  
     //getters and setters
  
  
  
  @Entity
  @Table(name = &quot;counters_data&quot;)
  public class CountersData {
      @Id
      @GeneratedValue(generator = &quot;inc&quot;)
      @GenericGenerator(name = &quot;inc&quot;, strategy = &quot;increment&quot;)
      private int id;
      private long counterDataBegin;
      private long counterDataEnd;
  
      public CountersData() {
      }
  // getters adn setters
  </code></pre>