通过在某个包中添加所有类,在Hibernate中添加带注释的类。 JAVA

有没有办法循环(例如,通过)所有类在一些包中? 我必须在AnnotationConfiguration上添加addAnnotatedClass(Class c) 。 这样做:

  AnnotationConfiguration annotationConfiguration.addAnnotatedClass(AdditionalInformation.class); annotationConfiguration.addAnnotatedClass(AdditionalInformationGroup.class); annotationConfiguration.addAnnotatedClass(Address.class); annotationConfiguration.addAnnotatedClass(BankAccount.class); annotationConfiguration.addAnnotatedClass(City.class); //et cetera 

我的所有表都在Tables.Informations包中。

正如评论中所提到的,使用AnnotationConfiguration API无法加载包中所有类的function。 以下是您可以使用所述API执行的一些操作(请注意,“addPackage”方法仅读取包元数据,例如在package-info.java类中找到的,它不会加载包中的所有类):

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html

 sessionFactory = new AnnotationConfiguration() .addPackage("test.animals") //the fully qualified package name .addAnnotatedClass(Flight.class) .addAnnotatedClass(Sky.class) .addAnnotatedClass(Person.class) .addAnnotatedClass(Dog.class) .addResource("test/animals/orm.xml") .configure() .buildSessionFactory(); 

以下代码遍历指定包中的所有类,并列出使用“@Entity”注释的类。 这些类中的每一个都被添加到您的Hibernate工厂配置中,而不必明确地列出它们。

 public static void main(String[] args) throws URISyntaxException, IOException, ClassNotFoundException { try { Configuration configuration = new Configuration().configure(); for (Class cls : getEntityClassesFromPackage("com.example.hib.entities")) { configuration.addAnnotatedClass(cls); } sessionFactory = configuration.buildSessionFactory(); } catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } } public static List> getEntityClassesFromPackage(String packageName) throws ClassNotFoundException, IOException, URISyntaxException { List classNames = getClassNamesFromPackage(packageName); List> classes = new ArrayList>(); for (String className : classNames) { Class cls = Class.forName(packageName + "." + className); Annotation[] annotations = cls.getAnnotations(); for (Annotation annotation : annotations) { System.out.println(cls.getCanonicalName() + ": " + annotation.toString()); if (annotation instanceof javax.persistence.Entity) { classes.add(cls); } } } return classes; } public static ArrayList getClassNamesFromPackage(String packageName) throws IOException, URISyntaxException, ClassNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ArrayList names = new ArrayList(); packageName = packageName.replace(".", "/"); URL packageURL = classLoader.getResource(packageName); URI uri = new URI(packageURL.toString()); File folder = new File(uri.getPath()); File[] files = folder.listFiles(); for (File file: files) { String name = file.getName(); name = name.substring(0, name.lastIndexOf('.')); // remove ".class" names.add(name); } return names; } 

有用的参考: https : //stackoverflow.com/a/7461653/7255

您可以使用LocalSessionFactoryBuilder构建会话工厂,以便指定scanPackages属性。

 SessionFactory sessionFactory = new LocalSessionFactoryBuilder(hikariDataSource()) .scanPackages("com.animals.entities") .addProperties(properties) .buildSessionFactory();