我的后端应用程序通过REST API获取json对象,这个对象存在于数据库中而不存在于Caeynne ObjectContext中,如何通过ObjectContext按id删除对象。
// <dependency>
// <groupId>org.apache.cayenne</groupId>
// <artifactId>cayenne-server</artifactId>
// <version>4.0.M5</version>
// </dependency>
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.configuration.server.ServerRuntime;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cayenne.test.model.Artist;
@RestController
@RequestMapping(value = "/rest")
public class ArtistRestController {
@DeleteMapping(value = "/artist")
public ResponseEntity deleteArtist(@RequestBody Artist artist) {
ServerRuntime runtime = ServerRuntime
.builder()
.addConfig("cayenne-cayenne_test.xml")
.build();
ObjectContext context = runtime.newContext();
// don't work
context.deleteObject(artist);
context.commitChanges();
return new ResponseEntity<>(HttpStatus.OK);
}
}发布于 2017-08-23 17:35:29
context.localObject(myObject);
context.deleteObject(myObject);
context.commitChanges();MyObject myObject = Cayenne.objectForPk(context, MyObject.class, id);
context.deleteObject(myObject);
context.commitChanges();在这种情况下,您的对象可能会从数据库中获取,以恢复它的实际状态,并跟踪可以与此对象一起删除的所有关系。
https://stackoverflow.com/questions/45832136
复制相似问题