title: retrieve data of collection type from database using getters and setters
body: <p>i am having three classes one is USER, second Vehicle and third is notfound_test.<br>
  In USER class i declared vehicle as Collection as follow.<br>
      <strong>Collection vehicle=new ArrayList();</strong><br>
  And also having getters and setters of it.<br>
  So, my question is how i can retrieve data from database i am using hibernate?<br>
  I tried following code.<br>
  <strong>i am unable from getting data in JOPTIONPANE OF VEHICLE IN THIRD CLASS.i wanted the associated data</strong><br></p>
  
  <pre><code>package org.notfound.annotation;
  
  import java.util.ArrayList;
  import java.util.Collection;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.OneToMany;
  
  @Entity
  public class User {
  
  @Id @GeneratedValue
  private int id;
  private String name;
  @OneToMany
  Collection&lt;Vehicle&gt; vehicle=new ArrayList&lt;Vehicle&gt;();
  
  
  public Collection&lt;Vehicle&gt; getVehicle() {
      return vehicle;
  }
  public void setVehicle(Collection&lt;Vehicle&gt; vehicle) {
      this.vehicle = vehicle;
  }
  public int getId() {
      return id;
  }
  public void setId(int id) {
      this.id = id;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  
  
  }
  
  package org.notfound.annotation;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.ManyToOne;
  
  @Entity
  public class Vehicle {
  
  @Id @GeneratedValue
  private int vehicle_id;
  private String vehicle_name;
  @ManyToOne
  private User user=new User();
  
  
  
  public User getUser() {
      return user;
  }
  public void setUser(User user) {
      this.user = user;
  }
  public int getVehicle_id() {
      return vehicle_id;
  }
  public void setVehicle_id(int vehicle_id) {
      this.vehicle_id = vehicle_id;
  }
  public String getVehicle_name() {
      return vehicle_name;
  }
  public void setVehicle_name(String vehicle_name) {
      this.vehicle_name = vehicle_name;
  }
  
  
  }
  
   package org.notfound.annotation;
  
   import javax.swing.JOptionPane;
  
   import org.hibernate.Session;
   import org.hibernate.SessionFactory;
   import org.hibernate.cfg.Configuration;
  
   public class Notfound_test {
  
  
  public static void main(String[] args) {
  
      User user=new User();
      Vehicle vehicle=new Vehicle();
      Vehicle vehicle2=new Vehicle();
  
      user.setName("NIRAV J. KAMANI");
      vehicle.setVehicle_name("CHEVROLET CRUIZE");
      vehicle2.setVehicle_name("MARUTI ERTIGA");
      vehicle.setUser(user);
      vehicle2.setUser(user);
      user.getVehicle().add(vehicle);
      user.getVehicle().add(vehicle2);
  
      SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
      Session session=sessionfactory.openSession();
      /*session.beginTransaction();
      session.save(user);
      session.save(vehicle);
      session.save(vehicle2);
      session.getTransaction().commit();
      session.close();*/
  
      session.beginTransaction();
      user=(User) session.get(User.class, 1);
      vehicle=(Vehicle) session.get(Vehicle.class, 1);
      vehicle2=(Vehicle) session.get(Vehicle.class, 2);
      JOptionPane.showMessageDialog(null, user.getId()+" "+user.getName()+" "+user.getVehicle().toString(), "INFORMATION", JOptionPane.INFORMATION_MESSAGE);
      session.close();
  
  }
  
  }
  </code></pre>