title: How to get Field Variable on Java Spring
body: <p>I has a issue on my learn project, on case &quot;if condition value is null and then else if condition value field is null&quot; for example my code following these code :</p>
  <p>For Entity <strong>Users.java</strong> :</p>
  <pre><code>@Entity
  public class Users {
      private Long id;
      private String employeeId;
      private String fullName;
      private String username;
      private String password;
      ...
  
      public Users() {
      }
  
      Some Code Setter and Getter....
  }
  </code></pre>
  <p>For Entity <strong>Employee.java</strong> :</p>
  <pre><code>@Entity
  public Class Employee {
      private Long id;
      private String employeeId;
      private String fullName;
      ...
      
      public Employee() {
      }
      
      Some Code Setter and Getter....
  }
  </code></pre>
  <p>and then for my Class Service i have case for insert data Employee with Repository. On case we have validation data before insert data to table Employee, we need to check <strong>table users not null</strong> and then on field <strong>employeeId</strong> should null. with my code following this :</p>
  <p>For Repository <strong>UserRepo.java</strong> and <strong>EmployeeRepo.java</strong> :</p>
  <pre><code>@Repository
  public interface EmployeeRepo extends CrudRepository&lt;Employee, Long&gt; {
  
  }
  
  @Repository
  public interdace UsersRepo extends CrudRepository&lt;Users, Long&gt; {
  
  @Transactional
  @Modifying(clearAutomatically = true, flushAutomatically = true)
  @Query(&quot;UPDATE Users u SET u.employeeId = :employeeId WHERE u.id = :id&quot;)
  public void updateEmployeeIdUsers(@Param(&quot;id&quot;) Long id, @Param(&quot;employeeId&quot;) String employeeId);
  
  }
  </code></pre>
  <p>For Service <strong>UsersService.java</strong> :</p>
  <pre><code>@Service(&quot;usersService&quot;)
  public class UsersService {
      
      @Autowired
      private UsersRepo repo;
      
      public Optional&lt;Users&gt; findById(Long id) {
          return repo.findById(id);
      }
      
      public void updateEmployeeIdUsers(Long id, String employeeId) {
          repo.updateEmployeeIdUsers(id, employeeId);
      }
  
  }
  </code></pre>
  <p>For Service <strong>EmployeeService.java</strong> :</p>
  <pre><code>@Service(&quot;employeeService&quot;)
  public class EmployeeService {
      
      @Autowired
      private EmployeeRepo employeeRepo;
      
      @Autowired
      private UsersService userService;
      
      public Employee insertEmployee(Employee employee) throws Exception {
          Optional&lt;Users&gt; users = userService.findById(employee.getId());
          Users userOptional = new Users(); **//on this my problem**
          userOptional.getEmployeeId(); **//on this my problem**
          if (!users.isPresent()) {
              throw new Exception(&quot;User ID : &quot;+ employee.getId() +&quot; Not Founded&quot;);
          }else if (!(userOptional == null)) { **//on this my problem**
              throw new Exception(&quot;User employeID : &quot;+ employee.getEmployeeId() +&quot; Already Exist on Users&quot;);
          }
          
          String str1 = &quot;TEST&quot;;
          Long idUser = employee.getId();
          userService.updateEmployeeIdUsers(idUser, str1);
          return employeeRepo.save(employee);
      }
  
  }
  </code></pre>
  <p>on this code we have problem on else if userOptional is always NULL and i try to debug to see value on employeeId just i see always Null. so any idea with my problem because i try some case alway fail with my issue. please if any idea for my issue, can reply these my question. is very thank you of all about thing to answer my question.</p>