【问题标题】:Can hibernate scan packages to create SessionFactory automatically?hibernate 扫描包可以自动创建 SessionFactory 吗?
【发布时间】:2016-05-16 16:44:25
【问题描述】:

我可以将 Hibernate 配置为自动扫描包以从 @Entity 带注释的 bean 创建 SessionFactory 吗?

目前我正在使用

Configuration config = new Configuration() ;
config.addAnnotatedClass(MyEntity.class);

我不想使用hibernate.cfg.xml 来配置映射。

请注意,我想在不使用任何 Spring 或类似框架的普通 Java 项目中实现这一点。

之前已经使用 Spring 回答了类似的问题,但我想在不使用 Spring 或其他框架的情况下实现它。我对一些执行此操作的简单库持开放态度。

【问题讨论】:

    标签: java hibernate hibernate-mapping


    【解决方案1】:

    没有。即使使用最新的 Hibernate 5 版本,您也不能说 Hibernate 扫描包中的持久类。 Configuration 有方法addPackage(),但它用于读取“包级元数据”(.package-info- 文件)。

    你不想使用 Spring,那你能做什么:

    使用流畅的休眠

    你可以使用 EntityScanner 来自 fluent-hibernate 库(除了库,您不需要其他 jar)

    对于 Hibernate 4 和 Hibernate 5:

    Configuration configuration = new Configuration();
    EntityScanner.scanPackages("my.com.entities", "my.com.other.entities")
        .addTo(configuration);
    SessionFactory sessionFactory = configuration.buildSessionFactory();
    

    使用新的 Hibernate 5 引导 API:

    List<Class<?>> classes = EntityScanner
            .scanPackages("my.com.entities", "my.com.other.entities").result();
    
    MetadataSources metadataSources = new MetadataSources();
    for (Class<?> annotatedClass : classes) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }
    
    SessionFactory sessionFactory = metadataSources.buildMetadata()
        .buildSessionFactory();
    

    使用其他库

    如果您已经使用了可用于扫描的库,例如Reflections,则有一个测试项目,其中包含使用各种库进行实体扫描的示例:hibernate-scanners-test

    【讨论】:

      【解决方案2】:
       public static  ArrayList listfiles(String pckgname) {
             ArrayList classes=new ArrayList();
      try{
      
          // Get a File object for the package 
          File directory=null; 
          try { 
            directory=new File(Thread.currentThread().getContextClassLoader().getResource(pckgname.replace('.', '/')).getFile()); 
          } catch(NullPointerException x) { 
            System.out.println("Nullpointer");
            throw new ClassNotFoundException(pckgname+" does not appear to be a valid package"); 
          } 
          if(directory.exists()) { 
            // Get the list of the files contained in the package 
            String[] files=directory.list(); 
            for(int i=0; i<files.length; i++) { 
      // we are only interested in .class files 
      if(files[i].endsWith(".class")) { 
        // removes the .class extension 
        classes.add(Class.forName(pckgname+'.'+files[i].substring(0, files[i].length()-6))); 
      } 
            } 
          } else { 
      System.out.println("Directory does not exist");
            throw new ClassNotFoundException(pckgname+" does not appear to be a valid package"); 
          } 
          Class[] classesA=new Class[classes.size()]; 
          classes.toArray(classesA); 
      
           //       return classesA;
      
      } catch (Exception e) {
      e.printStackTrace();
      }
      return classes;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-16
        • 1970-01-01
        • 2019-10-22
        • 1970-01-01
        • 2017-10-23
        • 2013-08-11
        相关资源
        最近更新 更多