Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringRoo:将JPA实体用作DAO_Jpa_Dao_Spring Roo - Fatal编程技术网

SpringRoo:将JPA实体用作DAO

SpringRoo:将JPA实体用作DAO,jpa,dao,spring-roo,Jpa,Dao,Spring Roo,下面的代码是默认情况下生成的代码,EntityManager与管理实体的其余方法(保存、更新、删除、findXXX等)一起注入到域模型POJO中 也许这是一种更面向对象的方法(与之相比),但我不明白的是: 在每个实体中注入EntityManager时是否存在性能问题(假设从数据库中检索1000个实体) 事务管理(@transactional annotations)不应该放在服务层中吗?(假设您希望以原子方式使用两个不同的实体) 您能考虑一下这段代码相对于经典DAO层的其他优点/缺点吗 代码

下面的代码是默认情况下生成的代码,EntityManager与管理实体的其余方法(保存、更新、删除、findXXX等)一起注入到域模型POJO中

也许这是一种更面向对象的方法(与之相比),但我不明白的是:

  • 在每个实体中注入EntityManager时是否存在性能问题(假设从数据库中检索1000个实体)

  • 事务管理(@transactional annotations)不应该放在服务层中吗?(假设您希望以原子方式使用两个不同的实体)

  • 您能考虑一下这段代码相对于经典DAO层的其他优点/缺点吗

代码如下所示(为了清晰起见,删除了一些方法):

@可配置
@实体
@爪哇豆
@生根
@RooEntity
公开课答案{
@持久上下文
瞬态实体管理器;
@交易的
公共图书馆{
如果(this.entityManager==null)this.entityManager=entityManager();
this.entityManager.persist(this);
}
@交易的
公共空间删除(){
如果(this.entityManager==null)this.entityManager=entityManager();
if(this.entityManager.contains(this)){
this.entityManager.remove(this);
}否则{
Answer attached=Answer.findAnswer(this.id);
此.entityManager.remove(已连接);
}
}
@交易的
公共应答合并(){
如果(this.entityManager==null)this.entityManager=entityManager();
答案合并=this.entityManager.merge(this);
this.entityManager.flush();
返回合并;
}
公共静态最终EntityManager EntityManager(){
EntityManager em=新答案()。EntityManager;
如果(em==null)抛出新的IllegalStateException(“尚未注入实体管理器(SpringAspectsJAR是否配置为AJC/AJDT方面库?”);
返回em;
}
公共静态长计数答案(){
返回entityManager();
}
公共静态列表findAllAnswers(){
返回entityManager();
}
...
}
  • 您将在第三点的链接中找到更多关于此的信息

  • 在典型的Roo应用程序中没有服务层。您的服务方法包含在实体本身中,因此可以在实体中使用
    @Transactional
    ,以确保特定方法涉及到事务。然而,您将能够使用最新的SpringRoo 1.2版本获得一个单独的服务层,这将使它成为可能

  • ADM vs.DDD:关于SO的单独问题将有助于解决这一问题。无论如何,你可以在SpringSource Roo论坛上通过这个帖子获得很多见解


  • 干杯,祝Roo一切顺利!:)

    谢谢你的链接。它唯一没有真正回答的问题是第一个。不清楚EntityManager何时以及如何注入每个实体。@GuidoGarcía,你现在有答案了吗?@GuidoGarcía现在呢?@shams没有,我不再使用Roo了。
    @Configurable
    @Entity
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Answer {
    
        @PersistenceContext
        transient EntityManager entityManager;
    
        @Transactional
        public void persist() {
            if (this.entityManager == null) this.entityManager = entityManager();
            this.entityManager.persist(this);
        }
    
        @Transactional
        public void remove() {
            if (this.entityManager == null) this.entityManager = entityManager();
            if (this.entityManager.contains(this)) {
                this.entityManager.remove(this);
            } else {
                Answer attached = Answer.findAnswer(this.id);
                this.entityManager.remove(attached);
            }
        }
    
        @Transactional
        public Answer merge() {
            if (this.entityManager == null) this.entityManager = entityManager();
            Answer merged = this.entityManager.merge(this);
            this.entityManager.flush();
            return merged;
        }
    
        public static final EntityManager entityManager() {
            EntityManager em = new Answer().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 countAnswers() {
            return entityManager().createQuery("SELECT COUNT(o) FROM Answer o", Long.class).getSingleResult();
        }
    
        public static List<Answer> findAllAnswers() {
            return entityManager().createQuery("SELECT o FROM Answer o", Answer.class).getResultList();
        }
    
        ...
    }