11

Can I configure Hibernate to scan packages automatically to create a SessionFactory from @Entity annotated beans ?

Currently I am using

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

I do not want to use hibernate.cfg.xml to configure mappings.

Please note I want to achieve this in a plain Java project without using any Spring or such frameworks.

Similar question have been answered using Spring before but I want to achieve it without using Spring or other frameworks. I am open to some simple library that does this.

2 Answers 2

10

No. You can't say Hibernate to scan packages for persistent classes even with the last Hibernate 5 version. Configuration has method addPackage(), but it is for reading "package-level metadata" (.package-info- files).

You don't want to use Spring, so what can you do:

Using fluent-hibernate

You can use EntityScanner from fluent-hibernate library (you will not need to have other jars, except the library)

For Hibernate 4 and Hibernate 5:

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

Using a new Hibernate 5 bootstrapping 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();

Using other libraries

If you already use a library that can be used for scanning, for an example Reflections, there is a test project with examples of using various libraries for entity scanning: hibernate-scanners-test.

0
 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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.