javahibernate

Add Annotated Class in Hibernate by adding all classes in some package. JAVA


is there any way to loop (e.g. via for) all classes with are in some package? I have to addAnnotatedClass(Class c) on AnnotationConfiguration. Doing it like this:

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

All of my tables are in package Tables.Informations.


Solution

  • As mentioned in the comments, the functionality of loading all classes in a package is not possible with the AnnotationConfiguration API. Here's some of the stuff you can do with said API (note that the "addPackage" method only reads package metadata, such as that found in the package-info.java class, it does NOT load all classes in package):

    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();