-1

This is a line I've encountered inside a DAO class:

pages.addAll((List<Page>)getCurrentSession()
        .createCriteria(Page.class)
        .add(Restrictions.eq("path", pagePath))
        .setMaxResults(1).list());

I've never seen a method call right after a set of brackets, with no dot in between. Is it just an optional different syntax, or does it do something beyond getting the session associated with the List?

(Note: Using Java / Spring, and Hibernate @Transactional business here)

EDIT: updated to the complete line of code, didn't realise it was significant. Mostly I was thrown by the method-like syntax without the dot.


Most probably you missed the complete line

(List<Object>) getCurrentSession().createCriteria(Page.class).list();

Returns a list of Page objects (pojo's) from database.

Your line

pages.addAll((List<Page>)getCurrentSession().createCriteria(Page.class)
        .add(Restrictions.eq("path", pagePath))
        .setMaxResults(1).list()); 

1)pages is a list

2)

 (List<Page>)getCurrentSession()
                .createCriteria(Page.class)
                .add(Restrictions.eq("path", pagePath))
                .setMaxResults(1).list()

Here the list() method on criteria Object return a list of Objects and here you are casting back them to Page objects,Since you know that they are Page objects

is a criteria to fetch

3) addAll method adding all results to pages

4

2 回答 2

2

Briefly, the statement

getCurrentSession().createCriteria(Page.class)
    .add(Restrictions.eq("path", pagePath))
    .setMaxResults(1).list()

returns a List which is then casted to

List<Page>

Then, the casted object is passed to the page.addAll() method.

于 2013-09-17T13:44:41.383 回答
1

很可能你错过了完整的线路

(List<Object>) getCurrentSession().createCriteria(Page.class).list();

从数据库返回Page 对象列表(pojo)。

你的线

pages.addAll((List<Page>)getCurrentSession().createCriteria(Page.class)
        .add(Restrictions.eq("path", pagePath))
        .setMaxResults(1).list()); 

1)pages是一个列表

2)

 (List<Page>)getCurrentSession()
                .createCriteria(Page.class)
                .add(Restrictions.eq("path", pagePath))
                .setMaxResults(1).list()

这里list()标准对象的方法返回一个对象列表,在这里你将它们转换回页面对象,因为你知道它们是页面对象

是获取的标准

3)addAll 方法将所有结果添加到pages

于 2013-09-17T13:43:02.493 回答