问题
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