Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA merge entity - from web restful jackson serialized entity
    primarykey
    data
    text
    <p>The project is using springmvc building the restful service, and JPA persist data into DB.</p> <p>I am recently working on update one object through web interface, i use jackson to deserialize post json data into object class. However, I have problem to merge object back to database. I sort of knew the object class deserialized from jackson is unmanaged, that is the reason I merge is nothing happen. Here is the sample code.</p> <p>Entity:</p> <pre><code>@Entity @Table(name="Parent") public class Parent{ @Id @GeneratedValue(strategy = GenerationType.AUTO) @JsonView(JsView.Public.class) @Column(name = "id") private Long Id; @NotNull @JsonView(JsView.Public.class) private String name; @NotNull @JsonView(JsView.Public.class) private String description; @NotNull @ManyToOne(fetch= FetchType.LAZY, cascade = {CascadeType.REFRESH}) @JoinColumn(name="creator_id") @JsonView(JsView.Internal.class) private User creator; @OneToMany(mappedBy="quest", cascade = {CascadeType.REFRESH}, orphanRemoval = true) private List&lt;Collaboration&gt; collaborators = new ArrayList&lt;Collaboration&gt;(); </code></pre> <p>}</p> <p>Restful Controller:</p> <pre><code> @RequestMapping(value = "/parent/{id}/update", method = RequestMethod.PUT) public ResponseEntity&lt;String&gt; updateQuestAttribute(@RequestBody String parentJson,@PathVariable("id") long parent_id) { ObjectMapper mapper = new ObjectMapper(); Parent parent = null; Parent newParent = mapper.readValue(parentJson, Parent.class); updateParent = parentService.updateParent(questNew,parent_id); </code></pre> <p>Service layer:</p> <pre><code>@PersistenceContext private EntityManager entityMgr; @Transactional public Parent updateParent(Parent newp, long id){ Parent old = findOne(id); Parent update = entityMgr.merge(newp); return update; } </code></pre> <p>I have tested this method with some managed object in Junit, all went fine. So, I guess all entity merge code is find in normal situation. Can anybody advise me what the problem is? thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload