Re: Possibility to create a jpa query in frontend

瀏覽次數:77 次
跳到第一則未讀訊息

Timo Westkämper

未讀,
2013年2月21日 凌晨4:55:012013/2/21
收件者:Querydsl on behalf of Daniel Görlich
Hi Daniel.

On Thu, Feb 21, 2013 at 11:19 AM, Daniel Görlich via Querydsl <querydsl+noreply-APn2wQetaHJLPqn...@googlegroups.com> wrote:
Hi

Is it possible to use the queryDSL in frontend? E.g.
I have 4 filters in the frontend. I want to create for each filter a
standard query. Only the column names and the values will be
replaced with the user input. Then i will pass the query to the
backend, put the entityManager into it and execute the query. Is this
possible? If not can you add a feature like this?

Yes, it's possible. You can easily create dynamic queries with for example the PathBuilder class.

What kind of query model on the frontend side do you have in mind? Something JSON based?

Br,
Timo
 

Currently I only found this:
HQLQuery query = new JPAQuery (entityManager);
...

Is it possible to set the entityManger later?

Best regards

Daniel Görlich

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


Daniel Görlich

未讀,
2013年2月21日 清晨5:37:122013/2/21
收件者:quer...@googlegroups.com
Hi Timo,

thx for fast reply.

I use JSF in my frontend and I want to create typeSave queries. I initialize the filter and set the table and the fields which i want to filter. I press a button to create and execute the query and get a resultList.

Example:

Person class (Backend - Entity)
    firstName
    lastName
    age
    gender
   
Query class (Backend)
   
    private List<?> executeQuery(HQLQuery query){
        query.setEntityManager(em);
        //execute query and return result
    }
   
Filter class (Frontend)
   
    private Person person; // my backend entity where the user sets the filter values
    private QPerson qPerson; //generated queryDSL object
    private Field? firstNameField ;
    private Field? lastNameField;
...
   
    public void initFilter(){
        qPerson = QPerson.person;
        firstNameField = person.firstname
        lastNameField = person.firstname
...
    }
   
   
    public HQLQuery createQuery(table, firstNameField, lastNameField){
        HQLQuery query = new JPAQuery();
        query.from(table);
       
        if (person.firstName != null && !person.firstName.trim().isEmpty()){
            query.where(firstNameField.eq(person.firstName))
        }
        //and so on
        return query;
    }

    public void executeQuery(){
      query = createQuery(qPerson, firstNameField, lastNameField);
//call backend and pass query
    }

Is this idea possible with queryDSL or can i only create unsaved dynamic queries?

Timo Westkämper

未讀,
2013年2月21日 清晨5:44:022013/2/21
收件者:Querydsl on behalf of Daniel Görlich
Hi.


Yes, but there is a price for the typesafety in generic queries. There will be deep if / else structures if you want to use the generated Q-types for such kind of queries.

I'd advise you to create them really dynamically. Querydsl supports the dynamic model as well.

Br,
Timo

 

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniel Görlich

未讀,
2013年2月21日 清晨5:58:122013/2/21
收件者:quer...@googlegroups.com
So you wouldn't do it typesafe, because the performance is bad?

Can you please give me a link to an example with the dynamic model, thx.

Daniel

Timo Westkämper

未讀,
2013年2月21日 清晨6:35:432013/2/21
收件者:Querydsl on behalf of Daniel Görlich
Hi Daniel.

On Thu, Feb 21, 2013 at 12:58 PM, Daniel Görlich via Querydsl <querydsl+noreply-APn2wQetaHJLPqn...@googlegroups.com> wrote:
So you wouldn't do it typesafe, because the performance is bad?

No, I meant that there is lots of code you need to write, and it will be difficult to maintain. The performance difference won't be big.
 

Can you please give me a link to an example with the dynamic model, thx.

Ok, let's assume you have an entity type, a property name and a value.

Using PathBuilder (http://www.querydsl.com/static/querydsl/2.9.0/apidocs/com/mysema/query/types/path/PathBuilder.html) it would be something like this

  PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class, "entity");
  query.where(entity.get(propertyName).eq(value));
  List<Entity> result = query.list(entity);

If you have a map of property and value pairs then it might become something like this

  for (Map.Entry<String, Object> entry : values) {
      query.where(entity.get(entry.getKey()).eq(entry.getValue()));
  }

For lt, gt etc filters you need to use other getters of PathBuilder, but for eq, ne filters, get(String) works just fine.

Br,
Timo

 

Daniel

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniel Görlich

未讀,
2013年2月21日 清晨7:30:362013/2/21
收件者:quer...@googlegroups.com
Hi Timo,

thanks for the example and your time :)

But I think it's the same code isn't it? Here I also have to check if the value is null or empty. If it is null or empty i don't want to add it to the query. So where is the difference? I have to pass the propertyName and tableName in both ways.

In my case I have a "filter template". I create a NamedBean (JSF class)  which have the properties for a filter (e.g. it's a periodFilter). So my NamedBean have the properties dateFrom, dateTo. Also my NamedBean has a method which creates automatically the where clause e.g. where propertyName > dateFrom and propertyName < dateTo (if one of the property is null don't add it -> where propertyName > dateFrom). This is one method which includes this functionality and creates the where-statement.

Now i can use this filter template for each view where I have to filter a column which includes a date. And I only have to pass the propertyName (property which should be filtered) and the query where the statement should be added.

I don't understand why I need more code if i use the typsafeQuery instead the other one.

Another question: Can you also use the PathBuilder for classes which are not entitites e.g. classes which are created at runtime?

Daniel

Timo Westkämper

未讀,
2013年2月21日 清晨7:42:102013/2/21
收件者:Querydsl on behalf of Daniel Görlich
Hi.

On Thu, Feb 21, 2013 at 2:30 PM, Daniel Görlich via Querydsl <querydsl+noreply-APn2wQetaHJLPqn...@googlegroups.com> wrote:
Hi Timo,

thanks for the example and your time :)

But I think it's the same code isn't it? Here I also have to check if the value is null or empty. If it is null or empty i don't want to add it to the query. So where is the difference? I have to pass the propertyName and tableName in both ways.

In my case I have a "filter template". I create a NamedBean (JSF class)  which have the properties for a filter (e.g. it's a periodFilter). So my NamedBean have the properties dateFrom, dateTo. Also my NamedBean has a method which creates automatically the where clause e.g. where propertyName > dateFrom and propertyName < dateTo (if one of the property is null don't add it -> where propertyName > dateFrom). This is one method which includes this functionality and creates the where-statement.

If you have a typed representation of your query then this works as you described.

If you want a more generic Query representation then use one of the patterns I described.
 

Now i can use this filter template for each view where I have to filter a column which includes a date. And I only have to pass the propertyName (property which should be filtered) and the query where the statement should be added.

I don't understand why I need more code if i use the typsafeQuery instead the other one.

Ok, let's suppose you have a typed query with value1, value2 and value3 values which are bound to property1, property2 and property3.

So there will be max three conditions added to the query and all of them with explicit typesafe code.

Do you agree that you need to change your code, write more code, if you add support for a fourth parameter?

 

Another question: Can you also use the PathBuilder for classes which are not entitites e.g. classes which are created at runtime?

No, I don't think that works.

Br,
Timo
 


Daniel

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniel Görlich

未讀,
2013年2月21日 上午8:02:242013/2/21
收件者:quer...@googlegroups.com
Yes I agree, but in my case I will only have "typed representation" of a query. In some cases I can add the parameter if it is null in some not and so on.. I have to implement it once and that's it (these are old filter templates which haven't be changed the last years, I only have to implement it with a newer technology). So it doesn't matter if I use typsafe queries or not, right?

Which way would you prefer to create statements for "dynamic classes"? Is there also a possibility to do it with queryDSL?

Thanks for your support :)

Daniel

Timo Westkämper

未讀,
2013年2月21日 上午8:11:522013/2/21
收件者:Querydsl on behalf of Daniel Görlich
Hi Daniel

On Thu, Feb 21, 2013 at 3:02 PM, Daniel Görlich via Querydsl <querydsl+noreply-APn2wQetaHJLPqn...@googlegroups.com> wrote:
Yes I agree, but in my case I will only have "typed representation" of a query. In some cases I can add the parameter if it is null in some not and so on.. I have to implement it once and that's it (these are old filter templates which haven't be changed the last years, I only have to implement it with a newer technology). So it doesn't matter if I use typsafe queries or not, right?

Yes.
 

Which way would you prefer to create statements for "dynamic classes"? Is there also a possibility to do it with queryDSL?

If I'd be submitting queries to Querydsl based backends over HTTP, I'd use a JSON based tree to describe the data.

And the Querydsl code would use similar patterns as shown below.
 

Thanks for your support :)


Br,
TImo


Daniel

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniel Görlich

未讀,
2013年2月21日 上午8:17:192013/2/21
收件者:quer...@googlegroups.com
Thx and have a nice day :)
回覆所有人
回覆作者
轉寄
0 則新訊息