java - JPA persisting instead of updating -
I have come across a strange problem, whenever I try to edit a register in my application instead of the unit To update the situation, it remains a new one. Here my update
method
goes public obituary (object obj) {EntityManagerFactory fac = persistence .createEntityManagerFactory ("crud"); EntityManager em = fac.createEntityManager (); Em.merge (obj); Em.getTransaction () start () .; Committed to em.getTransaction (). Em.close (); Fac.close (); }
My guess is that when I submit the edit form and the setters are called Id
, somehow is set to 0. Is this possible? The form looks like this:
& lt; H: form & gt; & Lt; P: panel grid column = "2" style = "margin: 0 auto;" & Gt; & Lt; F: Aspect Name = "Header" & gt; Edit form & lt; / F: Aspect & gt; Name: & lt; H: inputText id = "nome" value = "# {editUserBean.userToEdit.name}" /> Age: & lt; H: Input Text = "# {editUserBean.userToEdit.age}" converter = "javax.faces.Integer" /> Near: & lt; H: Input Text = "# {editUserBean.userToEdit.password}" /> Gender: & lt; H: selectOneRadio value = "# {editUserBean.userToEdit.gender}" style = "font-size: 12px;" & Gt; & Lt; F: selection item item item = "mask" item label = "mask" /> & Lt; F: selection item item value = "fame" item label = "fame" /> & Lt; / H: selectOneRadio & gt; & Lt; F: aspect names = "footer" & gt; & Lt; Div align = "center" & gt; & Lt; H: command button value = "save" functionalist = "# {editUserBean.save}" icon = "UI-icon-check" /> & Lt; / Div & gt; & Lt; / Ch: Aspect & gt; & Lt; / P: panelGrid & gt; & Lt; / H: form & gt;
Hibernate
is inquiring about this:
Hibernate: Select hibernate_shnese. Dual hibernate: users (age, gender, name, password, id) value (?,?,?,?,?)
The real problem is in the JSF layer
All the evidence suggests that editUserBean
is scoped the request . This means that you will get a new Bean per request.
Therefore, we are working on each request with a new userToEdit
Bean. Its property can be either a default value for zero
or primitives (e.g., 0
for int
and long
) As long as you do not explicitly declare a default value for the property inside beans, it contains id
which is 0
in your case.
When you merge your bean, then there is no firm reference with the ID / code> id or related entries in the database, so the merge
is issued If you are creating value for the primary key, there are obstacles that you will never be in the form of 0
as id
, therefore the JPA layer is new Will continue to continue the institutions
In a simple fix for this problem The user information is said to be stored in the bean, session scod or user defined scoped conversation :
@ conversation.Scoped class UserToEdit user {// ... or create one relationship if you want}
Then you can inject your bean to normal and stored id
will remain alive between requests (Do not forget to clean; fix the limits of the conversation correctly and / or the session Clean bean).
Another workaround - if you want to keep user information in the scope of the request - then to get the value of id
Using hidden fields (as well as any non-editable property you want). In this way the user will also be submitted on the next request and the model will be updated correctly.
& lt; H: InputHide value = "# {editUserBean.userToEdit.id}" />
Warning: However this solution is very popular, only if the current user can update the information of another user? In order to submit a fake request containing id
for changing user.id
and / or misusing the system, malicious users can easily manipulate the dom.
Comments
Post a Comment