Menu

Liferay 6.2: Custom Dynamic query with array parameters

Liferay Service Builder (LSB) is a great tool for Liferay developers. We use the methods of the services which is generated by LSB in the most of the case. But, you can not work with multi-table. There was a solution in this case (click here to see). This article is a good guide but you cannot execute a query with array parameters. So, executing a custom dynamic query with array parameters is the purpose of this post.

Problem

I assume that we have to filter some Post by specific authors (click here to get details). At the moment, we have an array (or a list) of authorIds and we need to answer the question "how to write the query and pass this argument".

Implement

I assumed that you have implemented the example in this article. Now, I am going to add a new method which named findByAuthors in PostFinderImpl class. This method has three arguments, they are an array of authorIds, start and end index. And the return value of this method is a list of Post whose authorId is an element of the authorIds argument.
There is the detail of code.

As you see, method findByAuthors ranged from line 25th to line 81st. And the lines from 37th to 43rd are the most important. There are how much element of the authorIds array will correspond with the number of the ? character. For example: if the array contains two elements, the generating query is 'SELECT {ExampleBlog_Post.*} FROM ExampleBlog_Post WHERE AuthorId in (?, ?)'
And the last, running ant build-service and using your method. You can download my source code on github. And have fun!

Không có nhận xét nào:

Đăng nhận xét