hiberate 4.3 - 实体类实例不在列表中返回 [英] hiberate 4.3 - entity class instances not returned in list
问题描述
我没有使用Spring的情况下使用Hibernate 4.3.8。我使用Hibernate的会话API。我有一个实体类 Category
,我用 @Entity
, @Table
, @Id
, @Column
等等。我不使用.hbm.xml描述符文件,我只是想在我的域/实体java源文件/类文件中使用注释。
1)OK,I以这种方式创建我的hibernate SessionFactory
:
Configuration configuration = new Configuration ()。配置();
// configuration.addClass(Category.class);
StandardServiceRegistryBuilder builder =
Standard StandardServiceRegistryBuilder()。applySettings(configuration.getProperties());
configuration.addPackage(com.test.db.domain);
factory = configuration.buildSessionFactory(builder.build());
2)然后当我试试这个时:
<
我刚刚收到一个空列表(我期待得到数据库表中的所有类别)。 Category
类位于com.test.db.domain包中。
可能是什么这是为什么? 3)请注意,如果我使用 session.createSQLQuery
我可以连接到我的数据库并获取所有类别。 4)另外请注意,我不想使用Hibernate的EntityManager API,JPA和与JPA相关的XML描述符。
问题原来如下:调用 configuration.addPackage
并没有做到我最初期望的那样。另见:
而不是调用 addPackage
应该做的是从他自己的实体/域类中为每个类调用 configuration.addAnnotatedClass
。
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.
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.
这篇关于hiberate 4.3 - 实体类实例不在列表中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!