/**
  * Perform Spring-based scanning for entity classes, registering them as annotated classes with
  * this {@code Configuration}.
  *
  * @param packagesToScan one or more Java package names
  * @throws HibernateException if scanning fails for any reason
  */
 public void scanPackages(String... packagesToScan) throws HibernateException {
   try {
     for (String pkg : packagesToScan) {
       String pattern =
           ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
               + ClassUtils.convertClassNameToResourcePath(pkg)
               + RESOURCE_PATTERN;
       Resource[] resources = resourcePatternResolver.getResources(pattern);
       MetadataReaderFactory readerFactory =
           new CachingMetadataReaderFactory(resourcePatternResolver);
       for (Resource resource : resources) {
         if (resource.isReadable()) {
           MetadataReader reader = readerFactory.getMetadataReader(resource);
           String className = reader.getClassMetadata().getClassName();
           if (matchesFilter(reader, readerFactory)) {
             addAnnotatedClasses(resourcePatternResolver.getClassLoader().loadClass(className));
           }
         }
       }
     }
   } catch (IOException ex) {
     throw new MappingException("Failed to scan classpath for unlisted classes", ex);
   } catch (ClassNotFoundException ex) {
     throw new MappingException("Failed to load annotated classes from classpath", ex);
   }
 }
예제 #2
0
  public static void main(String[] args) throws Exception {
    try {
      ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

      AnnotationConfiguration config =
          new AnnotationConfiguration()
              .configure(new File("D:/gjxt/source/news/config/hibernate/hibernate.cfg.xml"));
      config.setProperty("output", "C:\\ddl.txt");

      String[] packagesToScan = {"com.team.cmc.domain.model"};
      for (String pkg : packagesToScan) {
        String pattern =
            ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                + ClassUtils.convertClassNameToResourcePath(pkg)
                + "/**/*.class";
        Resource[] resources = resourcePatternResolver.getResources(pattern);
        MetadataReaderFactory readerFactory =
            new CachingMetadataReaderFactory(resourcePatternResolver);
        for (Resource resource : resources) {
          if (resource.isReadable()) {
            MetadataReader reader = readerFactory.getMetadataReader(resource);
            String className = reader.getClassMetadata().getClassName();
            if (matchesFilter(reader, readerFactory)) {
              config.addAnnotatedClass(
                  resourcePatternResolver.getClassLoader().loadClass(className));
            }
          }
        }
      }

      SessionFactory sessionFactory = config.buildSessionFactory();
      session = sessionFactory.openSession();
      tx = session.beginTransaction();

      SchemaExport schemaExport = new SchemaExport(config);
      schemaExport.setOutputFile("c:\\a.txt");
      schemaExport.create(true, true);

      System.out.println("Table created.");

      tx.commit();

      //
      // config = new Configuration().configure(new File(
      // "src/hibernate.cfg.xml"));
      //
      // System.out.println("Creating tables...");
      //
      // SessionFactory sessionFactory = config.configure()
      // .buildSessionFactory();
      // session = sessionFactory.openSession();
      // tx = session.beginTransaction();
      //
      // SchemaExport schemaExport = new SchemaExport(config);
      // schemaExport.create(true, true);
      //
      // System.out.println("Table created.");
      //
      // tx.commit();

    } catch (HibernateException e) {
      e.printStackTrace();
      try {
        tx.rollback();
      } catch (HibernateException e1) {
        e1.printStackTrace();
      }
    } finally {

    }
  }
 public ClassLoader getClassLoader() {
   return fallback.getClassLoader();
 }