title: Getting only last record in Hibernate but i want all records
body: <p>I am using Hibernate with Spring Rest. I have two tables in my database named user_table and user_profession. user_table contains user info and primary key is id. user_profession contains the profession of user with u_id foreign key. The working of project is that i pass u_id into user_profession and get a profession of user now from that profession i fetch all the u_id from user_profession table which have that profession . And now i got many u_id from user_profession and now i map user_profession with user_table using @OneToOne mapping and got the user info corresponding to that u_id.</p>
  
  <p>It is working fine if a single u_id has single profession in user_profession but if a u_id has multiple entries with multiple professions then i am getting only last profession result while i want that if i pass a u_id into user_profession table and if that u_id has two entries with two professions then it could search u_id for both the professions but it is fetching only second profession u_ids.</p>
  
  <p>Here is My Entity classes for both tables. First is <strong>Profession.java</strong> for user_profession table</p>
  
  <pre><code>@Entity
  @Table(name="user_profession")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class Profession implements Serializable {
  
  
  private static final long serialVersionUID = 1L;
  
  
  @Id
  @GeneratedValue
  @Column(name="id")
  private long id;
  
  @Column(name="profession")
  private String profession;
  
  @Column(name="u_id")
  private String u_id;
  
  
  @OneToOne
  @JoinColumn(name = "u_id", referencedColumnName = "id", insertable=false, updatable=false)
  private User user;
  
  
  //Getters and setters
  
  
  public User getUser() {
      return user;
  }
  
  
  public void setUser(User user) {
      this.user = user;
  }
  
  
  public long getId() {
      return id;
  }
  
  
  public String getU_id() {
      return u_id;
  }
  
  public void setU_id(String u_id) {
      this.u_id = u_id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  public String getProfession() {
      return profession;
  }
  
  public void setProfession(String profession) {
      this.profession = profession;
  }
  
  }
  </code></pre>
  
  <p>Here is <strong>User.java</strong> for user_table</p>
  
  <pre><code>@Entity
  @Table(name="user_table")
  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  public class User implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue
      @Column(name="id")
      private long id;
  
      @Column(name="full_name")
      private String fullName;
  
      @Column(name="username")
      private String username;
  
  
  public long getId() {
      return id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  
  public String getFullName() {
      return fullName;
  }
  
  public void setFullName(String fullName) {
      this.fullName = fullName;
  }
  
  public String getUsername() {
      return username;
  }
  
  public void setUsername(String username) {
      this.username = username;
  }
  
  
  }
  </code></pre>
  
  <p>Here is my <strong>DAO</strong> class</p>
  
  <pre><code>    @SuppressWarnings("unchecked")
      public List&lt;Profession&gt; getProfessionById(long id) throws Exception {
      session = sessionFactory.openSession();
        session.beginTransaction();
        String queryString = "from Profession where u_id = :id";
        Query query = session.createQuery(queryString);
        query.setLong("id", id);
  
        List&lt;Profession&gt; professionList = query.list();
  
          String queryString1 = "from Profession where profession = :pro";
            Query query1 = null;
  
            for(Profession professionList1 : professionList){
              query1=session.createQuery(queryString1);
              query1.setString("pro", professionList1.getProfession());
  
            }
  
        session.flush();
        session.getTransaction().commit();
        return query1.list();
  }
  </code></pre>