Java spring mvc using hibernate to do update
<p>I started to develop APIs for iOS app using Java Spring MVC two months ago.
I'll explain my question with an example for clarification purpose.</p>

<p>Let's say I want to update a user's name.
My mySQL user table has columns: id, user_id, email, display_name.
My approach was:</p>

<ol>
<li>define user:</li>
</ol>

<p>User.java:</p>

<pre><code>package bean;
public class User {

  String displayName;
  String email;
  String userId;

  getters/setters... 
}
</code></pre>

<p>2.define a DAO:</p>

<p>UserDao.java:</p>

<pre><code>package dao;
import bean.StandardResponse;

public interface UserDao {
    public StandardResponse updateUserName(String user_id,String userName);
}
</code></pre>

<p>UserDaoImpl.java:</p>

<pre><code>package implement;
import bean.User;
import common.DatabaseConnect;

public UserDaoImpl implements UserDao {
    private DatabaseConnect dbConnect; 

    public UserDaoImpl(DatabaseConnect dbConnect) {
      this.dbConnect = dbConnect;
    }

    public StandardResponse updateUserName(userId,userName) {
      if ((userId == null || userId.isEmpty()) ||(userName == null || userName.isEmpty())) return new StandardResponse("Error","Parameters not set!");
      String sql = null;
      Statement smt = dbConnect.createDataBaseConnectResourse();
      StandardResponse result = new StandardResponse("Error","Fail to update the record!");
      sql = "update User set user_name="+userName+" where user_id='"+userId+"'";
      int updateResult = 0;
      try {
        updateResult = smt.executeUpdate(sql);
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } finally {
        dbConnect.closeDatabaseConnectResource();
      }
      if (updateResult == 1) {
        result = new StandardResponse("Success","The record has been updated!");
      }

      return result;        
  }
}
</code></pre>

<p>3.controller</p>

<pre><code>import org.springframework.stereotype.Controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import bean.StandardResponse;
import bean.User;
import common.DatabaseConnect;
import common.SpringApplicationContext;
import dao.UserDao;
import implement.UserDAOImpl;

@Controller
@RestController
@RequestMapping("user")
public class UserController {

  DatabaseConnect dbConnect = SpringApplicationContext.getApplicationContext().getBean("databaseConnect", DatabaseConnect.class);
  UserDao uiObject = new UserDaoImpl(dbConnect);

  @RequestMapping(value = "/updateUserName", method = RequestMethod.POST)
  public StandardResponse updateUserName(HttpServletRequest request, HttpServletResponse reponses, Model model) {
    StandardResponse srObject = uiObject.updateUserName(request.getparameter("userId"),request.getParameter("userName"));
    return srObject;
  }
}
</code></pre>

<p>I just put the crucial classes here. I believe you can understand what I am doing here. So if someone access the URL:****/user/updateUserName  providing the userId and userName, he can update the user name of that record. It is functionalbe.</p>

<p>I used the same approach and finished the whole project. It is working. Then, I asked an experienced programmer to look at my code since I figured out all these based on my own understanding. I would like to know how did they do in industry. He gave me some valuable comments.</p>

<ol>
<li>The whole structure is wrong. I shouldn't have logics in my dao. I should at least have dao, service and action layers. dao only handles database access, service handles all the logic and action handels communication and decide which service to call.</li>
<li>It is very bad approach to hand written SQL in the code. I should use Hibernate.</li>
<li>I should use control inverse and dependency injection.(I am not dealing with this in this post.)</li>
</ol>

<p>So I started to update my codes to use Hibernate.</p>

<ol>
<li>define user:</li>
</ol>

<p>User.java:</p>

<pre><code>package model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="User")
public class User {

  @Id
  @Column(name="id")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;

  @Column(name = "user_id")
  private String userId;

  @Column(name = "user_name")
  private String displayName;

  @Column(name = "email")
  private String emai;

  all getters/setters...
}
</code></pre>

<ol start="2">
<li>dao:</li>
</ol>

<p>UserDao.java:</p>

<pre><code>package dao;
import model.User;
import java.util.List;
public interface UserDAO {

  public void updateUser(User p);
  other methods....

}
</code></pre>

<p>UserDaoImpl.java</p>

<pre><code>package dao;
import model.User;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import model.User;

@Repository
public class PersonDAOImpl implements PersonDAO {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }

    @Override
    public void updatePerson(Person p) {
        Session session = this.sessionFactory.getCurrentSession();
        session.update(p);
    }

    implementations of other methods....

}
</code></pre>

<ol start="3">
<li>service:</li>
</ol>

<p>UserService.java:</p>

<pre><code>package service;

import java.util.List;

import model.User;

public interface UserService {

    public void updateUser(User p);
    other methods....

}
</code></pre>

<p>UserServiceImpl.java:</p>

<pre><code>package service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.UserDAO;
import model.User;

@Service
public class UserServiceImpl implements UserService {

    private UserDAO userDAO;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Override
    @Transactional
    public void updateUser(User p) {
       this.userDAO.updateUser(p);
    }

    implementation of other methods...

}
</code></pre>

<p>Now, I just need to write a class to handle the request and call this updateUser service. The whole structure looks better than mine. However, the request won't gave me the whole user object. All I can get is user_id and user_name. I am also not allowed to put logics in dao. So I have to query the table first to get the whole user object and then update the user_name. Comparing to what I used to do, one SQL handles the update. Now using Hibernate, I need 1 query and 1 update. </p>

<p>I found a solution on StackOverflow that I can use HQL to do this in one step. But I though the purpose for using Hibernate is to free us from writing the SQL. If I need to write HQL, why don't I just keep using the writing SQL approach.</p>

<p><strong>So is there a way to do update with Hibernate without query the table first in this structure? Or this is a trade-off for better structure?</strong></p>

<p>I am sorry that I used a really long example for this question. But I couldn't think of other ways to explain the whole story clearly. </p>