我正在尝试更新指向列表的实体 (EntityObjectContainigList)。我从数据库中获取实体,分离它,从实体指向的列表中删除一些元素,然后尝试将更新的实体与驻留在数据库中的实体合并。
现在的问题是,当我运行 em.merge(EntityObjectContainigList) 时,列表引用的对象/实体(我删除的那些)仍然留在数据库中。
@Entity
public class EntityObjectContainigList {
@Id
// @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "eocl", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
List<ContainerObject> list = new ArrayList<ContainerObject>();
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<ContainerObject> getList() {
return list;
}
public void setList(List<ContainerObject> list) {
this.list = new ArrayList<ContainerObject>(list);
}
}
ContainerObject 看起来像这样:
@Entity
public class ContainerObject {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonIgnore
private Key id;
@ManyToOne(fetch = FetchType.LAZY)
EntityObjectContainigList eocl;
private String s;
private int i;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
servlet 看起来像这样:
@Path("/db")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyResourcse {
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("transactions-optional");
@PUT
public Response putInDb(EntityObjectContainigList d) {
EntityManager em = null;
try {
em = emf.createEntityManager();
/* Gson g = new Gson();
System.out.println(g.toJson(d));*/
em.persist(d);
return Response.ok().build();
} finally {
if (em != null && em.isOpen()) em.close();
}
}
@GET
public Response getFromDb(@QueryParam("id")Long id) {
EntityManager em = null;
try {
em = emf.createEntityManager();
/* Gson g = new Gson();*/
EntityObjectContainigList o = em.find(EntityObjectContainigList.class, id);
/* System.out.println(g.toJson(o));*/
return Response.ok(o).build();
} finally {
if (em != null && em.isOpen()) em.close();
}
}
@POST
public Response updateDb(EntityObjectContainigList d) {
EntityManager em = null;
try {
em = emf.createEntityManager();
Gson g = new Gson();
d = em.merge(d);
System.out.println(g.toJson(d));
return Response.ok().build();
} finally {
if (em != null && em.isOpen()) em.close();
}
}
}
客户端看起来像这样:
public class UpdateClient {
public static void main(String[] args) {
ClientConfig cc = new DefaultClientConfig();
cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(cc);
WebResource res = client.resource("http://localhost:8888/rest/db");
EntityObjectContainigList o = new EntityObjectContainigList();
o.setId(12L);
o.setName("hello");
for (int i = 0; i < 5; i++) {
ContainerObject c = new ContainerObject();
c.setI(i);
c.setS("foo" + i );
o.getList().add(c);
}
res.type(MediaType.APPLICATION_JSON).put(ClientResponse.class, o);
System.out.println("Object inserted");
o = res.queryParam("id", "12")
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class)
.getEntity(EntityObjectContainigList.class);
System.out.println("Object fetched");
o.getList().remove(1);
o.getList().remove(0);
o.setName("world");
res.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, o);
System.out.println("Object updated");
System.out.println("Done.");
}
}
我将 GAE 1.7.2 与 JPA 2.0 一起使用。
任何帮助深表感谢。维塔利