休眠标准投影

发布于 2024-09-19 08:41:47 字数 1639 浏览 10 评论 0原文

正如问题标题所说,我正在尝试制定一个仅查询几个表属性的投影标准。

所以我有一个 Person 表/类,它有大约 40 个属性。我希望我的标准能够获得动态数量的属性,比方说 10、11 或 12(SQL 术语 从人中选择名字、姓氏),我是这样做的:

Transaction tx = session.beginTransaction();
Criteria crit = session.createCriteria(Person.class);
crit.setCacheable(true);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id"));
Criterias c = null;
 for (int i = 0; i < checked.size(); i++) {
        Attribute attr = checked.elementAt(i);
        switch (attr) {
            case LASTNAME:
                projList.add(Projections.property("lastName"));
                c = enumMap.get(attr);
                if (c.isChanged()) {
                    String tmp = (String) c.getAnswer();
                    tmp = tmp.replace('*', '%');
                    crit.add(Restrictions.like("lastName", tmp));
                    crit.addOrder(Order.asc("lastName"));
                }
            case ...THE REST .....
            }
    crit.setProjection(projList);
    retList = crit.list();
    tx.commit();
    return retList;

它返回了 < code>retList 元素不是来自 Person.class

信息 [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object;@14b9b80
致命的 [AWT-EventQueue-0] (Login.java78) - java.lang.ClassCastException: [Ljava.lang.Object;无法转换为 usergroupmanager.model.db.Person java.lang.ClassCastException:[Ljava.lang.Object;无法转换为 usergroupmanager.model.db.Person

请帮忙,现在我列出了所有 40 多个属性,它占用了查询时间,我不喜欢它。我也在寻找一种替代解决方案,它将帮助我解决这个问题。我读过有关 ResultTransformer 的内容,但还没有找到如何在我的案例中使用它。

Well as the question title says, I am trying to make a projection criteria querying only couple of the table attributes.

So I have a Person Table/class and it has about 40 attributes. I want my criteria to get dynamical number of attributes, lets say 10, 11 or 12 (SQL terms select firstname, lastname from person) and I was doing it like this:

Transaction tx = session.beginTransaction();
Criteria crit = session.createCriteria(Person.class);
crit.setCacheable(true);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id"));
Criterias c = null;
 for (int i = 0; i < checked.size(); i++) {
        Attribute attr = checked.elementAt(i);
        switch (attr) {
            case LASTNAME:
                projList.add(Projections.property("lastName"));
                c = enumMap.get(attr);
                if (c.isChanged()) {
                    String tmp = (String) c.getAnswer();
                    tmp = tmp.replace('*', '%');
                    crit.add(Restrictions.like("lastName", tmp));
                    crit.addOrder(Order.asc("lastName"));
                }
            case ...THE REST .....
            }
    crit.setProjection(projList);
    retList = crit.list();
    tx.commit();
    return retList;

and it gives back that the retList elements are not from the Person.class:

INFO [AWT-EventQueue-0] (UserGroupManagerApp.java127) - [Ljava.lang.Object;@14b9b80
FATAL [AWT-EventQueue-0] (Login.java78) - java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to usergroupmanager.model.db.Person

Please help, for now I am listing all the 40+ attr, and it takes up querying time and I do not like it. I am looking also an alternative solution which will help me solve this. I read about ResultTransformer but havent found how to use it in my case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩饰不了的爱 2024-09-26 08:41:47

您可以使用 criteria.setResultTransformer()

Hibernate 中提供了一些 Transformer。如果您的 Person 没有任何关联,请使用此:

criteria.setResultTransformer(Transformers.aliasToBean(Person.class));

但如果 Person 有任何关联,请考虑使用 http://github 上的 Seimos。 com/moesio/seimos

如果使用它代替 Criteria,可以节省大量代码。

You can use criteria.setResultTransformer()

There's some Transformers provided in Hibernate. If your Person has not any association use this:

criteria.setResultTransformer(Transformers.aliasToBean(Person.class));

But if Person has any association, consider use Seimos at http://github.com/moesio/seimos

A lot of code could be saved if you use it instead Criteria.

悟红尘 2024-09-26 08:41:47

[Ljava.lang.Object;不能投射到
usergroupmanager.model.db.Person

用简洁的语言表示 Object[] 无法转换为 Person。当您进行投影时,您将获得您选择的属性作为对象数组而不是水合实体。

您的代码缺少 retlist 声明。我猜这是一个原始的 List ,您可以将其转换为 List 某处。只需将其替换为 List 即可。

[Ljava.lang.Object; cannot be cast to
usergroupmanager.model.db.Person

Says in clean words Object[] cannot be cast to Person. When you do a projection, you will get the attributes you selected as an array of objects instead of a hydrated entity.

Your code is missing the declaration of retlist. I guess it's a raw List which you cast to a List<Person> somewhere. Just replace that with List<Object[]>.

萌能量女王 2024-09-26 08:41:47

如果您在 Hibernate 中使用投影,则不会查询 Hibernate 创建对象所需的所有数据。因此 Hibernate 无法创建对象。

因此,来自投影的查询仅返回从查询返回的 SQL 数组,即它作为列表返回,并且您可以作为该数组中的普通条目访问字段。

If you use a projection in Hibernate you are not querying all the data Hibernate needs to create the objects. Thus Hibernate cannot create the objects.

Thus the query from a projection just returns an array of the SQL returned from the query ie it returns a s List and you access the fields as plain entries in that array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文