1

附图显示了我的情况:在此处输入图像描述

在此处输入图像描述

每个程序都有自己的文档,每个文档都有自己的类型。

我有很多20多个程序,所以每个程序有60多个表。我必须显示,添加,删除和更新相关的文档,......所以我多次编写相同的代码,代码是这样的:

 //******************list des document liés à l'étape
     Session se = HibernateSessionFactory.getSession();
     Criteria cr = se.createCriteria(TypeDocFinDetachement.class);
     cr.add(Restrictions.eq("step",step));
     List<TypeDocFinDetachement> list = cr.list();

     //---------------------------------------procédure fin détachement en question
     FinDetachementDAO finDetachementDAO = new FinDetachementDAO();
     FinDetachement finDetachement=null;

     if(sidFinDetachement!="")
     { Integer  idFinDetachement = Integer.parseInt(sidFinDetachement);
       finDetachement = finDetachementDAO.findById(idFinDetachement);
     }

      //*************list des document déjà scannées
       Session sess = HibernateSessionFactory.getSession();
       Criteria criteria = sess.createCriteria(DocumentFinDetachement.class);
       //criteria.add(Restrictions.eq("finDetachement",finDetachement));
       criteria.createAlias("typeDocFinDetachement", "t");
       criteria.add(Restrictions.eq("t.step",step));
       List<DocumentFinDetachement> listdoc = criteria.list();
       Iterator<DocumentFinDetachement> it = listdoc.iterator();
       Iterator<TypeDocFinDetachement> iterator = list.iterator();

我正在使用休眠框架的 J2EE 环境中工作。我必须为每个程序编写一个特定的代码,不知道是否有一种方法可以概括这一点?

你也可以参考这个问题:Conceptual issue working with hibernate

4

1 回答 1

1

似乎您有 3 种不同形式的实体。例如对于“程序”“FinDetament”:

  • TypeDocFin 分离
  • DocFin 分离
  • 鳍分离

目前尚不清楚为什么需要 3(显然,如果可以的话,减少数量)。无论如何,假设你确实需要所有 3...

  1. 为“type document”、“document”和“procedure”中的每一个引入一个通用的抽象祖先类,并使用@Inheritance(参见Hibernate Docs):

    @Entity
    @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)  // Other strategies would also work
    public abstract class TypeDocument {
         // put common document fields & methods here -
         // if you can work out what they are (they do exist - even if minor!)
    
         @Id
         Integer id;
    
         String step;
    
         // etc
    }
    
    @Entity
    @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)  // Other strategies would also work
    public abstract class Document {
         // put common document fields & methods here -
         // if you can work out what they are (they do exist - even if minor!)
    }
    
    @Entity
    @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)  // Other strategies would also work
    public abstract class Procedure {
         // put common document fields & methods here -
         // if you can work out what they are (they do exist - even if minor!)
    }
    
  2. 为您的实际实体扩展这些:

    @Entity
    public class TypeDocFinDetachement extends TypeDocument {
         // put specific fields & methods here
    }
    
    @Entity
    public class DocFinDetachement extends Document {
         // put specific fields & methods here
    }
    
    @Entity
    public class FinDetachement extends Procedure {
         // put specific fields & methods here
    }
    
  3. 为 JPA 逻辑引入一个通用的通用 DAO

    public abstract class ProcedureDAO<T extends TypeDocument, D extends Document, P extends Procedure> {
    
        public P findById(Integer id) {
            // put "procedure" query logic here
        }
    
        public void execProcedure(Class typeDocumentClass, 
                                           String typeDocName, 
                                           Class docClass, 
                                           String docName, 
                                           String step,
                                           String procedureId) {
            //******************list des document liés à l'étape
            Session se = HibernateSessionFactory.getSession();
            Criteria cr = se.createCriteria(typeDocumentClass);
            cr.add(Restrictions.eq("step",step));
            List<T> list = cr.list();
            Iterator<T> iterator = list.iterator();
    
            //------------------------ procédure fin détachement en question
            P procedure =null;
            if(sid != "") { 
                Integer  id = Integer.parseInt(procedureId);
                procedure = findById(id);
            }
    
            //*************list des document déjà scannées
            // Session sess = HibernateSessionFactory.getSession();
            Criteria criteria = sess.createCriteria(docClass);
            //criteria.add(Restrictions.eq(docName,doc));
            criteria.createAlias(typeDocName, "t");
            criteria.add(Restrictions.eq("t.step",step));
            List<D> listdoc = criteria.list();
            Iterator<D> it = listdoc.iterator();
    
            // etc
        }
    }
    
  4. 如果可能,将所有 JPA 逻辑放入祖先 DAO 并对其进行实例化,并将其称为“内联”。例如:

    public class SomeOtherClass {
    
        public void someMethod() {
            ProcedureDAO<TypeDocFinDetachement, DocFinDetachement, FinDetachement> finDetachementDAO = new ProcedureDAO();
            finDetachementDAO.execProcedure(TypeDocFinDetachement.class, "typeDocFinDetachement", DocumentFinDetachement.class, "finDetachement", "someStage");
       }
    }
    
  5. 如果 4 不可能,则创建扩展 DAO(但仅在绝对必要的情况下,因为逻辑必须发生重大变化):

    public class FinDetachementDAO extends ProcedureDAO<TypeDocFinDetachement, DocFinDetachement, FinDetachement> {
    
        // extend/override JPA logic here
    
    }
    
    public class SomeOtherClass {
    
        public void someMethod() {
            FinDetachementDAO finDetachementDAO = new FinDetachementDAO();
            finDetachementDAO.execProcedure(TypeDocFinDetachement.class, "typeDocFinDetachement", DocumentFinDetachement.class, "finDetachement", "someStage");
    
        }
    }
    
于 2013-06-25T10:17:20.123 回答