hiberate 4.3 - entity class instances not returned in list

Deadly 提交于 2019-12-12 03:25:12

问题


I am using Hibernate 4.3.8 without Spring. I am using Hibernate's session API. I have one entity class Category which I have annotated properly with @Entity, @Table, @Id, @Column and so on. I don't use .hbm.xml descriptor files, I just want to use the annotations in my domain/entity java source/class files.

1) OK, I create my hibernate SessionFactory in this way:

        Configuration configuration = new Configuration().configure();          
        // configuration.addClass(Category.class);
        StandardServiceRegistryBuilder builder = 
                new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        configuration.addPackage("com.test.db.domain");
        factory = configuration.buildSessionFactory(builder.build());

2) Then when I try this:

session.createCriteria(Category.class).list();

I just get an empty list back (I was expecting to get all categories that are in the DB table). The Category class is in the com.test.db.domain package.

What could be the reason for this? I am stuck for almost a day on this.

3) Note that if I use session.createSQLQuery I can connect to my DB and get all categories.

4) Also note that I don't want to use Hibernate's EntityManager API, JPA, and the XML descriptor(s) related to JPA.


回答1:


The problem turned out to be the following: calling configuration.addPackage does not do what I initially expected it to do. See also:

hibernate 4.3.x - load all entity annotated classes

Instead of calling addPackage what one should do is to call configuration.addAnnotatedClass for each class from his own entity/domain classes.



来源:https://stackoverflow.com/questions/28107513/hiberate-4-3-entity-class-instances-not-returned-in-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!