我正在使用 JPA (EclipseLink 2.5) 进行持久性。我的网络应用程序包含 AJAX 页面更新。保存帖子后,无需刷新浏览器即可显示发布的内容。该帖子具有使用 OneToMany 关系与之关联的标签。这些标签也应该在保存后显示。但是,由于启用了缓存,我无法获取标签。
这是我的代码:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Sqask save(AskTag tags) {
try {
Sqask ask = saveAsk(tags);
return ask;
} catch (Exception e) {
context.setRollbackOnly();
return null;
}
}
public Sqask saveAsk(AskTag tags) {
Sqask ask = new Sqask();
ask.setCreator(tags.getAskedBy());
em.persist(ask);
em.flush();
saveTags(tags, ask);
return ask;
}
public void saveTags(AskTag tags, Sqask ask) {
if (!tags.getNask().isEmpty()) {
NAsk nask = new NAsk();
NAskPK naskPK = new NAskPK();
naskPK.setAsk(ask.getId());
naskPK.setNoun(tags.getNask());
naskPK.setCreator(tags.getAskedBy().getGuid());
nask.setNAskPK(naskPK);
nask.setAsk(ask);
nask.setCreator(tags.getAskedBy());
em.persist(nask);
}
if (!tags.getVask().isEmpty()) {
VAsk vask = new VAsk();
VAskPK vaskPK = new VAskPK();
vaskPK.setAsk(ask.getId());
vaskPK.setVerb(tags.getVask());
vaskPK.setCreator(tags.getAskedBy().getGuid());
vask.setVAskPK(vaskPK);
vask.setAsk(ask);
vask.setCreator(tags.getAskedBy());
em.persist(vask);
}
if (!tags.getForask().isEmpty()) {
PAsk forask = new PAsk();
PAskPK foraskPK = new PAskPK();
foraskPK.setAsk(ask.getId());
foraskPK.setPrep(tags.getForask());
foraskPK.setCreator(tags.getAskedBy().getGuid());
forask.setPAskPK(foraskPK);
forask.setAsk(ask);
forask.setCreator(tags.getAskedBy());
em.persist(forask);
}
if (!tags.getAboutask().isEmpty()) {
PAsk aboutask = new PAsk();
PAskPK aboutaskPK = new PAskPK();
aboutaskPK.setAsk(ask.getId());
aboutaskPK.setPrep(tags.getAboutask());
aboutaskPK.setCreator(tags.getAskedBy().getGuid());
aboutask.setPAskPK(aboutaskPK);
aboutask.setAsk(ask);
aboutask.setCreator(tags.getAskedBy());
em.persist(aboutask);
}
em.getEntityManagerFactory().getCache().evict(Sqask.class, ask.getId());
em.merge(ask);
}
我怎样才能让事情发挥作用?如何刷新实体及其所有关联实体?谢谢。