java - updating entity containing object fields -
i'm trying update entity contains fields of type of class.
so entity:
@entity public class owner { @id @generatedvalue private int id; @column(name = "first_name") @notnull(message="{notnull}") @size(min=2,max=15,message="{size}") private string firstname; @notnull(message="{notnull}") @size(min=2,max=15,message="{size}") @column(name = "last_name") private string lastname; @valid @onetoone(cascade = cascadetype.all) private phone phone; @valid @onetoone(cascade = cascadetype.all) private pet pet;
from view:
<!doctype html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="iso-8859-1"></meta> <title>owner details</title> </head> <body> <div id="owner"> <form th:action="@{|/ownerlist/${owner.id}.do|}" th:object="${owner}" method="post"> <table> <tr> <td>id:</td> <td><input type="text" th:field="*{id}" /></td> <td th:if="${#fields.haserrors('id')}" th:errors="*{id}">fielderror</td> </tr> <tr> <td>first name</td> <td><input type="text" th:field="*{firstname}" /></td> <td th:if="${#fields.haserrors('firstname')}" th:errors="*{firstname}">fielderror</td> </tr> <tr> <td>last name</td> <td><input type="text" th:field="*{lastname}" /></td> <td th:if="${#fields.haserrors('lastname')}" th:errors="*{lastname}">fielderror</td> </tr> <tr> <td>phone</td> <td><input type="text" th:field="*{phone.number}" /></td> <td th:if="${#fields.haserrors('phone.number')}" th:errors="*{phones[0].number}">fielderror</td> </tr> <tr> <td>pet</td> <td><input type="text" th:field="*{pet.petname}" /></td> <td th:if="${#fields.haserrors('pet.petname')}" th:errors="*{pet.petname}">fielderror</td> </tr> <tr> <td><input type="submit" value="update" name="action" /></td> <td><input type="submit" value="delete" name="action" /></td> </tr> </table> </form> <a href="/ownerlist">back</a> </div> </body> </html>
i'm using controller:
@requestmapping(value = "/ownerlist/{id}.do") public string ownerdetailsdo(@modelattribute(value = "owner") owner owner, bindingresult result, @requestparam(value = "action") string action, model model) { switch (action) { case "update": objectbinder.bind(owner); ownerservice.update(owner); return "ownerdetail"; case "delete": ownerservice.remove(owner.getid()); model.addattribute("ownerlist", ownerservice.getall()); return "ownerlist"; } model.addattribute("owner", owner); return "ownerdetail"; }
so i'm trying update object of owner, inside of database after .merge can find new entity of , example phone, new id.
so make clear example have: owner: first name:xyz last name: bbb pet: bob phone: 1234
when try update phone, lets "2222", in db can find 2 records 1 "1234" second "2222", , want have "2222" replaced old 1 "1234".
if want delete phone when not referenced owner need add @orphanremoval.
orphan removal in relationships when target entity in one-to-one or one-to-many relationship removed relationship, desirable cascade remove operation target entity. such target entities considered “orphans,” , orphanremoval attribute can used specify orphaned entities should removed. example, if order has many line items , 1 of them removed order, removed line item considered orphan. if orphanremoval set true, line item entity deleted when line item removed order.
the orphanremoval attribute in @onetomany , @onetoone takes boolean value , default false.
the following example cascade remove operation orphaned customer entity when removed relationship:
@onetomany(mappedby="customer", orphanremoval="true") public list getorders() { ... }
@entity public class owner { @id @generatedvalue private int id; @column(name = "first_name") @notnull(message="{notnull}") @size(min=2,max=15,message="{size}") private string firstname; @notnull(message="{notnull}") @size(min=2,max=15,message="{size}") @column(name = "last_name") private string lastname; @valid @onetoone(cascade = cascadetype.all, orphanremoval="true") private phone phone; @valid @onetoone(cascade = cascadetype.all, orphanremoval="true") private pet pet;
and if this:
owner.set(new phone(2222)); entitymanager.merge(owner)); // update owner phone owner.set(new phone(77777)); //the phone(2222) deleted entitymanager.merge(owner));
Comments
Post a Comment