0
I want to make a "deposit" into the account with Id "x". The controller sends the "account" to the jsp that returns the deposit value to be updated in the database. That was the logic I used, but I think I was wrong about something, I believe in JSP or Controller. Because by clicking on "deposit" goes to the next page as if everything had happened well but the BD is not updated. What logic should I have used?
Edit 1: At the beginning was occurring the error "Detached Entity passed to persist: br.com.bitbank.modelo.Client" using "persist". I searched a little and even though the "find" returns an Entity "maneged" the error continued, so I changed the command to "merge". There is no more error in the console, but the table "Account" wins another registration all "NULL".
+----+---------+--------+-------+
| id | agencia | numero | saldo |
+----+---------+--------+-------+
| 1 | 10 | 4645 | 0 | Cadastro normal
| 2 | NULL | NULL | 0 | Após tentar "depositar"
The link to my complete project: https://github.com/BryanMotta/FormacaoJava/tree/master/src/main/java/br/com/bitbank
The parts I’m modifying:
@Controller
@RequestMapping("/transacao")
public class TransacaoController {
@Autowired
private ClienteDAO clienteDAO;
@RequestMapping(method = RequestMethod.GET, value = "/deposita/{id}")
public ModelAndView form(@PathVariable("id") int id) {
ModelAndView modelAndView = new ModelAndView("transacao/transacao");
Cliente cliente = clienteDAO.find(id);
modelAndView.addObject("cliente", cliente);
return modelAndView;
}
@RequestMapping(method = RequestMethod.POST, value = "/deposita/{id}")
public ModelAndView deposita(@PathVariable("id") int id, Cliente cliente) {
ModelAndView modelAndView = new ModelAndView("redirect:/");
//Cliente cliente = clienteDAO.find(id);
clienteDAO.atualiza(cliente);
modelAndView.addObject("cliente", cliente);
return modelAndView;
}
Jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form action="${s:mvcUrl('TC#deposita').arg(0,cliente.id).build() }"
method="post"
modelAttribute="cliente">
<div>
<input type="number" name="conta.deposita" />
<button type="submit">Depositar</button>
</div>
</form:form>
</body>
</html>
DAO
@Repository
@Transactional
public class ClienteDAO {
@PersistenceContext
private EntityManager manager;
public void gravar(Cliente cliente) {
manager.persist(cliente);
}
public void atualiza(Cliente cliente) {
manager.merge(cliente);
}
public Cliente getProduto(Integer id) {
Cliente cliente = manager.find(Cliente.class, id);
return cliente;
}
public List<Cliente> listar() {
return manager.createQuery("select c from Cliente c", Cliente.class)
.getResultList();
}
public Cliente find(int id) {
return manager.find(Cliente.class, id);
}
gives some Exception?
– Lucas Miranda
Yes, using "persist" from the error "Detached Entity passed to persist: br.com.bitbank.modelo.Client", and using "merge" does not occur any in the console, but the table account gains a whole "NULL" registration. I will add this information in the main question.
– Bryan Motta
and its intention is to actually insert a new state or just update the original?
– Lucas Miranda
Update the original. When I register a new customer automatically his balance is set to 0, the deposit method would add a value to the balance.
– Bryan Motta
I get it, is that persist and merge do not serve for this, you commented there that when recovers by find by id the object comes Managed right? then it would just be changing the information of that object and give a commit, maybe even the commit need if that Transactional is working
– Lucas Miranda
I think I get it, I don’t need to use persist and merge in my controller. But how do I make the logic? It’s done in jsp or controller?
– Bryan Motta