通过添加一些包中的所有类,在Hibernate中添加带注释的类。 JAVA [英] Add Annotated Class in Hibernate by adding all classes in some package. JAVA

查看:100
本文介绍了通过添加一些包中的所有类,在Hibernate中添加带注释的类。 JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我必须 addAnnotatedClass(Class c)有没有办法循环(例如通过)所有的类? c $ c> on AnnotationConfiguration
这样做:

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

我所有的表格都在包装 Tables.Informations

解决方案

正如评论中提到的,加载所有类的功能软件包不可能使用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)//完全限定包名
.addAnnotatedClass(Flight.class)
.addAnnotatedClass(Sky.class)
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Dog.class)
.addResource(test / animals / orm.xml)
.configure()
.buildSessionFactory();


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.

解决方案

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

这篇关于通过添加一些包中的所有类,在Hibernate中添加带注释的类。 JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆