有没有办法循环(例如,通过for)所有类在某个包中?
我必须在addAnnotatedClass(Class c)
上AnnotationConfiguration
。
这样做:
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
个包中。
答案 0 :(得分:13)
正如评论中所提到的,使用AnnotationConfiguration API无法加载包中所有类的功能。以下是您可以使用所述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();
答案 1 :(得分:6)
以下代码遍历指定包中的所有类,并列出使用“@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<Class<?>> getEntityClassesFromPackage(String packageName) throws ClassNotFoundException, IOException, URISyntaxException {
List<String> classNames = getClassNamesFromPackage(packageName);
List<Class<?>> classes = new ArrayList<Class<?>>();
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<String> getClassNamesFromPackage(String packageName) throws IOException, URISyntaxException, ClassNotFoundException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ArrayList<String> names = new ArrayList<String>();
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;
}
答案 2 :(得分:2)
有一个很好的开源软件包,称为“ org.reflections”。您可以在这里找到它:https://github.com/ronmamo/reflections
使用该软件包,您可以扫描以下实体:
Reflections reflections = new Reflections("Tables.Informations");
Set<Class<?>> importantClasses = reflections.getTypesAnnotatedWith(Entity.class);
for (Class<?> clazz : importantClasses) {
configuration.addAnnotatedClass(clazz);
}
答案 3 :(得分:1)
您可以使用 LocalSessionFactoryBuilder 构建会话工厂,以便指定 scanPackages 属性。
SessionFactory sessionFactory = new LocalSessionFactoryBuilder(hikariDataSource())
.scanPackages("com.animals.entities")
.addProperties(properties)
.buildSessionFactory();