Skip to content

Instantly share code, notes, and snippets.

@vepo
Last active May 3, 2017 19:33
Show Gist options
  • Save vepo/a9c8d7103e17f4dff109735587d9cd83 to your computer and use it in GitHub Desktop.
Save vepo/a9c8d7103e17f4dff109735587d9cd83 to your computer and use it in GitHub Desktop.
Load Class Annotations with Scannotation for Hibernate
public class HibernateScannUtil {
public static void scanAndAddAnnotatedClasses(Configuration configuration) {
ClassLoader classLoader = User.class.getClassLoader();
URL url = ClasspathUrlFinder.findClassBase(User.class);
try {
AnnotationDB db = new AnnotationDB();
db.setScanClassAnnotations(true);
db.setScanFieldAnnotations(false);
db.scanArchives(url);
Set<String> annotatedClasses = db.getAnnotationIndex().get(Entity.class.getName());
for (String annotatedClass : annotatedClasses) {
logger.info("Loading class {} on Hibernate config.", annotatedClass);
configuration.addAnnotatedClass(classLoader.loadClass(annotatedClass));
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment