开箱即用 - 不。但是,您可以编写自己的代码来检测/注册带注释的类。如果您使用的是Spring,则可以扩展并执行以下操作:AnnotationSessionFactoryBean
@Override
protected SessionFactory buildSessionFactory() throws Exception {
ArrayList<Class> classes = new ArrayList<Class>();
// the following will detect all classes that are annotated as @Entity
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
// only register classes within "com.fooPackage" package
for (BeanDefinition bd : scanner.findCandidateComponents("com.fooPackage")) {
String name = bd.getBeanClassName();
try {
classes.add(Class.forName(name));
} catch (Exception E) {
// TODO: handle exception - couldn't load class in question
}
} // for
// register detected classes with AnnotationSessionFactoryBean
setAnnotatedClasses(classes.toArray(new Class[classes.size()]));
return super.buildSessionFactory();
}
如果你没有使用Spring(你应该是:-)),你可以编写自己的代码来检测适当的类,并用你的via方法注册它们。AnnotationConfiguration
addAnnotatedClass()
顺便说一句,除非您实际上在包级别声明了某些内容,否则没有必要映射包。