deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pedro Belmino <pedrobelm...@gmail.com>
Subject Re: Extends AbstractEntityRepository Create custom methods
Date Mon, 07 Mar 2016 12:11:55 GMT
Hi,
Wojciech Żaboklicki,

His solution solves my problem in part, also needs to be returned the total
count of records, this value must be shown on the screen. For example: Page
3/10 - Total records - 97

2016-02-26 12:14 GMT-03:00 Wojciech Żaboklicki <zabian99@gmail.com>:

> Hi Pedro,
> In fact, it is common to use dynamic filtering, and CriteriaSupport let you
> do it. The only problem for you, as I understand, is that you can't
> paginate your resultset, after it is returned as list of objects, and not
> QueryResult. But this use case is not so common (not for web solutions at
> least). You rather don't read resultset page by page in one request, but
> you show exact page per each request, and you can do this with
> CriteriaSupport also.
>
> Example code:
> @Repository
> public abstract class YourEntityRepository extends
> AbstractEntityRepository<YourEntity, Integer>
>         implements CriteriaSupport<YourEntity> {
>
> public List<YourEntity> find(YourEntitySearchParams searchParams, int
> firstResult, int maxResults) {
>             Criteria<YourEntity, YourEntity> criteria = criteria();
>             if (searchParams.nameLike != null) {
>                 criteria.like(YourEntity_.name, "%" + searchParams.nameLike
> + "%");
>             }
>             // appending criteria from other params here ...
>             return
>
> criteria.createQuery().setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
>         }
>
>         public static class YourEntitySearchParams {
>             public String nameLike;
>             public Date dateFrom;
>             public Date dateTo;
>         }
> }
>
>
>
> 2016-02-26 13:34 GMT+01:00 Pedro Belmino <pedrobelmino@gmail.com>:
>
> > THAT'S IT. Daniel Cunha is right. It's very common that a screen that
> uses
> > paging is filled with dynamic filters.
> > So the framework allow not make a dynamic query with paging ?
> >
> > 2016-02-26 9:20 GMT-03:00 Daniel Cunha <danielsoro@gmail.com>:
> >
> > > I don't know why the javadoc is not published for last version.
> > >
> > > Someone can see that?
> > >
> > > On Fri, Feb 26, 2016 at 4:18 AM, Daniel Cunha <danielsoro@gmail.com>
> > > wrote:
> > >
> > > > If I get it, he want to use QueryResult to have easy way to
> navigate[1]
> > > on
> > > > pages.
> > > > I proposed that he use the Criteria API, but it not sound good for
> he.
> > > >
> > > > I don't know a
> > > >
> > > > [1]
> > > >
> > >
> >
> https://deltaspike.apache.org/javadoc/1.5.2/org/apache/deltaspike/data/api/QueryResult.html#nextPage()
> > > >
> > > > On Fri, Feb 26, 2016 at 4:13 AM, John D. Ament <
> johndament@apache.org>
> > > > wrote:
> > > >
> > > >> Hi,
> > > >>
> > > >> Are you sure you need to return query result? Seems like you're
> > crossing
> > > >> boundaries.
> > > >>
> > > >> typedQuery("").setFirstResult(2).setMaxResults(4).getResultList()
> > > >>
> > > >> That should take care of the pagination for you.
> > > >>
> > > >> John
> > > >>
> > > >> On Fri, Feb 26, 2016 at 6:58 AM Pedro Belmino <
> pedrobelmino@gmail.com
> > >
> > > >> wrote:
> > > >>
> > > >> > Hi,
> > > >> > Daniel Cunha,
> > > >> >
> > > >> > I appreciate the support but this solution to make the logic
in
> the
> > > >> > business layer is not viable for me , I would have to combine
all
> > the
> > > >> > possible filters.
> > > >> >
> > > >> > I'm at that point, but still no solution.
> > > >> > public QueryResult<Person>
> > findByNameLikeIgnoreCaseAndAssigment(String
> > > >> > name, Assigment assigment, @FirstResult int start, @MaxResults
int
> > > >> > pageSize){ String queryString = "select p "+ " FROM Person p
" +
> > > "WHERE
> > > >> > p.name = :name and p.assigment = :assigment";
> > javax.persistence.Query
> > > >> > query
> > > >> > = entityManager().createQuery(queryString);
> > query.setParameter("name",
> > > >> > name); query.setParameter("assigment", assigment);
> > QueryResult<Person>
> > > >> d =
> > > >> > new DefaultQueryResult<>(new QueryBuilder() { @Override
protected
> > > Object
> > > >> > execute(CdiQueryInvocationContext ctx) {
> > > >> > ctx.applyJpaQueryPostProcessors(query); return
> > > ctx.executeQuery(query);
> > > >> }
> > > >> > }, cdiQueryContextHolder.get());// OCCURS NullPointerException
> > return
> > > >> d; }
> > > >> >
> > > >> > Can someone help me?
> > > >> >
> > > >> > 2016-02-25 15:11 GMT-03:00 Daniel Cunha <danielsoro@gmail.com>:
> > > >> >
> > > >> > > Hi Pedro,
> > > >> > >
> > > >> > > You can try that:
> > > >> > >
> > > >> > >
> > > >> >
> > > >>
> > >
> >
> https://github.com/apache/deltaspike/blob/master/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleRepository.java#L103
> > > >> > >
> > > >> > >
> > > >> >
> > > >>
> > >
> >
> https://github.com/apache/deltaspike/blob/master/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/QueryResultTest.java#L167
> > > >> > >
> > > >> > > So, you'll create methods on your repository, something
like:
> > > >> > > findByNameLikeIgnoreCase
> > > >> > > findByAssigment
> > > >> > > findByNameLikeIgnoreCaseAndAssigment
> > > >> > >
> > > >> > >
> > > >> > > So you can put your business logic on your business class.
> > > >> > >
> > > >> > > You'll call the method that you need:
> > > >> > >
> > > >> > > @Inject
> > > >> > > private RepositoryClass repository
> > > >> > >
> > > >> > > if (YOUR_LOGIC)
> > > >> > >   repository.findByNameLikeIgnoreCaseAndAssigment
> > > >> > > ...
> > > >> > >
> > > >> > > To be honest, I don't know if possible to do like you was
> trying.
> > > >> > > I needed checking it.
> > > >> > >
> > > >> > >
> > > >> > > On Thu, Feb 25, 2016 at 2:04 PM, Pedro Belmino <
> > > >> pedrobelmino@gmail.com>
> > > >> > > wrote:
> > > >> > >
> > > >> > > > Daniel,
> > > >> > > > OK to return an object of type List<Person>.
> > > >> > > >
> > > >> > > > However i need return an QueryResult<Person>,
because the
> > > >> pagination is
> > > >> > > > important for me.
> > > >> > > >
> > > >> > > >
> > > >> > > >
> > > >> > > > 2016-02-25 13:59 GMT-03:00 Daniel Cunha <danielsoro@gmail.com
> >:
> > > >> > > >
> > > >> > > > > Hi Pedro,
> > > >> > > > >
> > > >> > > > > JPA Criteria API Support sounds better for this
cases:
> > > >> > > > >
> > > >> > > >
> > > >> > >
> > > >> >
> > > >>
> > >
> >
> http://deltaspike.apache.org/documentation/data.html#JPACriteriaAPISupport
> > > >> > > > >
> > > >> > > > > On Thu, Feb 25, 2016 at 1:50 PM, Pedro Belmino
<
> > > >> > pedrobelmino@gmail.com
> > > >> > > >
> > > >> > > > > wrote:
> > > >> > > > >
> > > >> > > > > > Hello,
> > > >> > > > > > I need construct an method that receive dynamic
parameters
> > for
> > > >> HQL
> > > >> > > > query
> > > >> > > > > > construct.
> > > >> > > > > >
> > > >> > > > > > For example:
> > > >> > > > > > @Repository(forEntity = Person.class)
> > > >> > > > > > public abstract class PersonCustomRepository
extends
> > > >> > > > > > AbstractEntityRepository<Person, Long>
 {
> > > >> > > > > >
> > > >> > > > > >        public QueryResult<Person>
> > > >> > > > > > findByNameLikeIgnoreCaseAndAssigment(String
name,
> > > >> > > > > > Assigment assigment, @FirstResult int start,
@MaxResults
> int
> > > >> > > pageSize){
> > > >> > > > > > String query = "select p from person p WHERE
1 = 1 ";
> > > >> > > > > > if(name!=null&&!name.equals("")){
> > > >> > > > > >   query+= " and name = ?1 ";
> > > >> > > > > > }
> > > >> > > > > > if(assigment!=null){
> > > >> > > > > >   query+= " and assigment = ?2 ";
> > > >> > > > > > }
> > > >> > > > > > // WHAT IS NECESSARY CODE FOR RETURN  paginated
object of
> > type
> > > >> > > > > > QueryResult<Person>.
> > > >> > > > > > }
> > > >> > > > > > }
> > > >> > > > > >
> > > >> > > > > > I'm doing something wrong? I do otherwise?
> > > >> > > > > >
> > > >> > > > > > --
> > > >> > > > > >
> > > >> > > > > >
> > > >> > > > > >
> > > >> > > > > > *Pedro Belmino *System Analist
> > > >> > > > > > Laboratory of Mobile Computing and Design
> > > >> > > > > > Federal University of Ceará
> > > >> > > > > > Office: + 55 85 3366-9797
> > > >> > > > > > E-mail: pedrobelmino@great.ufc.br <
> > > >> franciscobernardo@great.ufc.br>
> > > >> > > > > >
> > > >> > > > >
> > > >> > > > >
> > > >> > > > >
> > > >> > > > > --
> > > >> > > > > Daniel Cunha
> > > >> > > > > https://twitter.com/dvlc_
> > > >> > > > > http://www.tomitribe.com
> > > >> > > > >
> > > >> > > >
> > > >> > > >
> > > >> > > >
> > > >> > > > --
> > > >> > > >
> > > >> > > >
> > > >> > > >
> > > >> > > > *Pedro Belmino *System Analist
> > > >> > > > Laboratory of Mobile Computing and Design
> > > >> > > > Federal University of Ceará
> > > >> > > > Office: + 55 85 3366-9797
> > > >> > > > E-mail: pedrobelmino@great.ufc.br <
> > franciscobernardo@great.ufc.br
> > > >
> > > >> > > >
> > > >> > >
> > > >> > >
> > > >> > >
> > > >> > > --
> > > >> > > Daniel Cunha
> > > >> > > https://twitter.com/dvlc_
> > > >> > > http://www.tomitribe.com
> > > >> > >
> > > >> >
> > > >> >
> > > >> >
> > > >> > --
> > > >> >
> > > >> >
> > > >> >
> > > >> > *Pedro Belmino *System Analist
> > > >> > Laboratory of Mobile Computing and Design
> > > >> > Federal University of Ceará
> > > >> > Office: + 55 85 3366-9797
> > > >> > E-mail: pedrobelmino@great.ufc.br <franciscobernardo@great.ufc.br
> >
> > > >> >
> > > >>
> > > >
> > > >
> > > >
> > > > --
> > > > Daniel Cunha
> > > > https://twitter.com/dvlc_
> > > > http://www.tomitribe.com
> > > >
> > >
> > >
> > >
> > > --
> > > Daniel Cunha
> > > https://twitter.com/dvlc_
> > > http://www.tomitribe.com
> > >
> >
> >
> >
> > --
> >
> >
> >
> > *Pedro Belmino *System Analist
> > Laboratory of Mobile Computing and Design
> > Federal University of Ceará
> > Office: + 55 85 3366-9797
> > E-mail: pedrobelmino@great.ufc.br <franciscobernardo@great.ufc.br>
> >
>
>
>
> --
> Pozdrawiam,
> Wojtek Żaboklicki
>



-- 



*Pedro Belmino *System Analist
Laboratory of Mobile Computing and Design
Federal University of Ceará
Office: + 55 85 3366-9797
E-mail: pedrobelmino@great.ufc.br <franciscobernardo@great.ufc.br>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message