Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest 使用JAX-RS进行更新的有效方法_Rest_Jpa_Jersey - Fatal编程技术网

Rest 使用JAX-RS进行更新的有效方法

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

我正在开发一个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 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);