0

いつ物事が切り離され、永続化コンテキストの範囲になるかについて、私は少し混乱しています。私のエンティティは、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);
    }
4

1 に答える 1