Rest 使用JAX-RS进行更新的有效方法
我正在开发一个JPA/Jersey web应用程序,想知道是否有更好的方法更新记录。目前,我正在做:Rest 使用JAX-RS进行更新的有效方法,rest,jpa,jersey,Rest,Jpa,Jersey,我正在开发一个JPA/Jersey web应用程序,想知道是否有更好的方法更新记录。目前,我正在做: @PUT @Path("update/{id}") @Produces("application/json") @Consumes("application/x-www-form-urlencoded") public Response createDevice( @PathParam("id") int id, @FormParam("name") String
@PUT
@Path("update/{id}")
@Produces("application/json")
@Consumes("application/x-www-form-urlencoded")
public Response createDevice(
@PathParam("id") int id,
@FormParam("name") String name,
@FormParam("type") int type
/*MultivaluedMap<String, String> formParams*/
) {
try {
Devices newDevice = entityManager.find(Devices.class, id);
if(name==null){name=newDevice.getName();}
if(type != newDevice.getType()){newDevice.setType(type);}
newDevice.setName(name);
//newDevice.setType(type);
entityManager.merge(newDevice);
return Response.status(201).entity(new ResponseObject("success")).build();
} finally {
entityManager.close();
}
}
将只更改传入的值
有更好的方法吗?为什么首先要检查平等性?没用。只需指定新值。另外,如果您没有打开entityManager,为什么要关闭它?调用merge有什么意义?实体管理器需要将新设备与JPA中具有相同id的设备合并,对吗?还要调用entityManager.merge,我想entityManager已经打开了。我也不知道我在哪里检查平等性。。。
entityManager.merge(newDevice);