title: How to pass userid from one table to another table in spring mvc using hibernate
body: <p>I'm using Spring security for the login. I have the <code>User.java</code> which contains user-details.</p>
  
  <pre><code> @Entity(name = "user_table")
   //@Table(name = "user_table")
   public class User {
  
      @Id
      @Column(name = "id")
      private String userId;
  
      @Column(name = "email" ,unique = true)
      private String userEmail;
  
      @Column(name = "password")
      private String userPassword;    
  
      //getter and setters
  }
  </code></pre>
  
  <p>I'm getting the whole data of the current user from the table by using spring security. This is the code:</p>
  
  <pre><code>   public User findUserByEmail(String email) {
  
         List&lt;User&gt; users = new ArrayList&lt;User&gt;();
         try{
               users = sessionFactory.getCurrentSession().createQuery("from user_table where email= ?").setParameter(0, email).list();
  
              System.out.println("user is " +users);
         }catch(Exception e){
                System.out.println(e.getMessage());
                e.printStackTrace();
         }
  
         if (users.size() &gt; 0) {
               return users.get(0);
         } else {
                  return null;
        }
    }
  
  
  
      @Override
      public User getCurrentUser() {
  
              Authentication auth = SecurityContextHolder.getContext()
                              .getAuthentication();
              User currentUser = new User();
              if (!(auth instanceof AnonymousAuthenticationToken)) {
                      UserDetails userDetails = (UserDetails) auth.getPrincipal();
                      System.out.println("User has authorities: "
                                      + userDetails.getAuthorities());
                      System.out.println("USERNAME::  "+userDetails.getUsername());
                      currentUser = findUserByEmail(userDetails
                                      .getUsername());
                      System.out.println("currentUser    "+currentUser);
                      System.out.println("currentUser    "+currentUser.getUserId());
  
                      return currentUser;
              }
              return null;
      }
  </code></pre>
  
  <p>What I want is to send the user id which I'm getting from <code>currentUser.getUserId()</code> to some other method. In that method I'm mapping to some other table like <code>user_detail</code> table where id is primary key. By sending id, I will get the other user_details which are not present in the user_table.</p>
  
  <p>This is my <code>UserDetail</code>:</p>
  
  <pre><code>@Entity(name = "user_detail")
  @Table(name = "user_detail")
  public class UserDetail {
  
      @Id
      @GeneratedValue
      @Column(name = "id")
      private String userId;
  
      //some other details like Address .
      //getter and setter.
  }
  </code></pre>
  
  <p>From controller I'm calling the above method like this:</p>
  
  <pre><code>UserService userService = new UserService();
  User user=userDao.getCurrentUser();
  String userId = user.getUserId();
  System.out.println(userId);
  UserDetail u=userDao.findUserById(userId);
  </code></pre>
  
  <p>and this is the method where I pass the current user id : </p>
  
  <pre><code>public UserDetail findUserById(String id) {
          // TODO Auto-generated method stub
          List&lt;String&gt; users = new ArrayList&lt;String&gt;();
          try{
  
          users = sessionFactory.getCurrentSession().createQuery("from user_detail where id= ?").setParameter(0, id).list();
  
          System.out.println("user is " +users);
          }catch(Exception e){
             System.out.println(e.getMessage());
             e.printStackTrace();
          }
  
          if (users.size() &gt; 0) {
                  return null;
          } else {
                  return null;
          }
      }
  </code></pre>
  
  <p>Now the result I'm getting here is null . Like user is null. What I'm doing wrong here?</p>