Last active
May 3, 2017 19:33
-
-
Save vepo/a9c8d7103e17f4dff109735587d9cd83 to your computer and use it in GitHub Desktop.
Load Class Annotations with Scannotation for Hibernate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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