title: Update object in relation by Hibernate ORM in Java
body: <p>I have question about access to data.
  I have that DB:</p>
  <pre><code>[country: id, country_name],
  [city: id, country_id, city_name],
  [address: id, shop_data_id, city_id, address_data],
  [shop_data: id, data]
  </code></pre>
  <p>My relations <code>country-city</code> one to many, <code>city-address</code> one to many, <code>address-shop_data</code> one to one.
  I'm looking for information that can I do that SQL query with ORM, or what is the best way do do it in ORM.</p>
  <pre><code>UPDATE shop_data 
  INNER JOIN country ON country.id=1
  INNER JOIN city ON country.id=city.country_id 
  INNER JOIN address ON city.id= address.city_id 
  INNER JOIN shop_data ON address.shop_data_id=shop_data.id 
  SET shop_data.data=&quot;shop data string&quot; 
  WHERE shop_data.id=address.shop_data_id
  </code></pre>
  <p>I know that in SQL I should start by <code>shop_data</code>, but by doing this I want to show that I want start in ORM by <code>country</code> entity.</p>
  <p>I wrote entities in Hibernate with annotation</p>
  <pre><code>@Entity
  @Table(name=&quot;country&quot;)
  public class Country{
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @Column(name = &quot;country&quot;)
      private String country;
      
      @OneToMany(mappedBy = &quot;country&quot;, fetch=FetchType.LAZY)
      @JsonBackReference
      private List&lt;City&gt; cities = new ArrayList&lt;&gt;();
      
      // getters/setters ..
  }
  
  @Entity
  @Table(name=&quot;city&quot;)
  public class City{
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @Column(name = &quot;city&quot;)
      private String city;
      
      @OneToMany(mappedBy = &quot;city&quot;, fetch=FetchType.LAZY)
      private List&lt;Address&gt; adresses = new ArrayList&lt;&gt;();
      
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name=&quot;country_id&quot;)
      @JsonIgnore
      @JsonManagedReference
      private Country country;
  
      // getters/setters ..
  }
  
  
  @Entity
  @Table(name=&quot;address&quot;)
  public class Address {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @Column(name = &quot;address&quot;)
      private String address;
      @Column(name = &quot;district&quot;)
      private String district;
      @Column(name = &quot;post_code&quot;)
      private String postCode;
  
      @OneToOne
      @JoinColumn(name=&quot;shop_data_id&quot;)
      private ShopData shopData;
      
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name=&quot;city_id&quot;)
      private City city;
      
      // getters/setters ..
  
  }
  
  @Entity
  @Table(name=&quot;shop_data&quot;)
  public class shopData {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      @Column(name = &quot;data&quot;)
      private String data;
      
      // getters/setters ..
  }
  </code></pre>
  <p>I know that I can use getters starting from <code>country</code> that returns <code>List&lt;ObjectType&gt;</code> and from there get my object. Next run function <code>update</code> and update right row. But in this way are done some number of queries.</p>
  <p>Is it possible to do by Java Hiberante ORM by one query? Or which way is the best to minimize query amount? By this method I also want to update next also address data.</p>