对于事物何时变得分离以及持久性上下文的范围,我有点困惑。我的实体是否仅在 roo 生成的每个方法中“管理”?我问是因为我已经读过在分离的实例上调用 refresh() 应该引发异常,但是然后我在我的实体上调用 refresh 而不先合并它我似乎没有收到错误...
有人可以根据实体管理器的生存时间以及对象在单个合并、持久化、删除方法中何时被管理和分离来解释 Roo 代码......
// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
// You may push code into the target .java compilation unit if you wish to edit any member(s).
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
privileged aspect AdminDirectRole_Roo_Jpa_ActiveRecord {
@PersistenceContext
transient EntityManager AdminDirectRole.entityManager;
public static final EntityManager AdminDirectRole.entityManager() {
EntityManager em = new AdminDirectRole().entityManager;
if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
return em;
}
public static long AdminDirectRole.countAdminDirectRoles() {
return entityManager().createQuery("SELECT COUNT(o) FROM AdminDirectRole o", Long.class).getSingleResult();
}
public static List<AdminDirectRole> AdminDirectRole.findAllAdminDirectRoles() {
return entityManager().createQuery("SELECT o FROM AdminDirectRole o", AdminDirectRole.class).getResultList();
}
public static AdminDirectRole AdminDirectRole.findAdminDirectRole(BigDecimal id) {
if (id == null) return null;
return entityManager().find(AdminDirectRole.class, id);
}
public static List<AdminDirectRole> AdminDirectRole.findAdminDirectRoleEntries(int firstResult, int maxResults) {
return entityManager().createQuery("SELECT o FROM AdminDirectRole o", AdminDirectRole.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}
@Transactional
public void AdminDirectRole.persist() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.persist(this);
}
@Transactional
public void AdminDirectRole.remove() {
if (this.entityManager == null) this.entityManager = entityManager();
if (this.entityManager.contains(this)) {
this.entityManager.remove(this);
} else {
AdminDirectRole attached = AdminDirectRole.findAdminDirectRole(this.id);
this.entityManager.remove(attached);
}
}
@Transactional
public void AdminDirectRole.flush() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.flush();
}
@Transactional
public void AdminDirectRole.clear() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.clear();
}
@Transactional
public AdminDirectRole AdminDirectRole.merge() {
if (this.entityManager == null) this.entityManager = entityManager();
AdminDirectRole merged = this.entityManager.merge(this);
this.entityManager.flush();
return merged;
}
}
任何建议表示赞赏。我创建了这个方法,期望它由于我没有将实体合并到任何持久性上下文中而失败,并且没有引发异常?
@Transactional
public void refresh() {
if (this.entityManager == null) this.entityManager = entityManager();
entityManager.refresh(this);
}