Is there a querydsl example with hibernate UserType like setting query parameters with UserType?

614 views
Skip to first unread message

Jian Wu

unread,
Jan 31, 2014, 1:41:03 PM1/31/14
to quer...@googlegroups.com
Hi,

I'm planning to use querydsl in our hibernate app project using hibernate mapping, which we have more than 10 UserTypes used, is there a querydsl example/test case showing how querydsl handling hibernate UserTypes?

Thanks in advance!

Jian 

timowest

unread,
Feb 3, 2014, 11:28:15 AM2/3/14
to quer...@googlegroups.com
Hi.

Querydsl should work out of the box with usertypes. What problems do you expect?

Timo

Jian Wu

unread,
Feb 7, 2014, 2:48:19 AM2/7/14
to quer...@googlegroups.com
Hi Timo,

I have two specific questions:

1. If we want to hide/prevent some Entity Properties associated with UserType from being used in query condition through querydsl, how can we change the codeine process? I can see, by default, querydsl has generated Q[UserType] through "EmbeddableSerializer" for each UserType

2. How set parameter associated with UserType through querydsl? will querydsl create CustomType from UserType internally?

Thanks a lot!

Jian   

Jian Wu

unread,
Feb 9, 2014, 4:13:27 AM2/9/14
to quer...@googlegroups.com
Any suggestion or tips would be really appreciated!

Jian

timowest

unread,
Feb 10, 2014, 2:13:28 PM2/10/14
to quer...@googlegroups.com
Hi.


On Friday, February 7, 2014 9:48:19 AM UTC+2, Jian Wu wrote:
Hi Timo,

I have two specific questions:

1. If we want to hide/prevent some Entity Properties associated with UserType from being used in query condition through querydsl, how can we change the codeine process? I can see, by default, querydsl has generated Q[UserType] through "EmbeddableSerializer" for each UserType

Could you provide an example of an entity and usertype with the top level annotations?

Normally user types should be treated differently.

2. How set parameter associated with UserType through querydsl? will querydsl create CustomType from UserType internally?

Do you mean a parameter with the type of a usertype or something else?

Timo
 

Jian Wu

unread,
Feb 11, 2014, 12:47:50 AM2/11/14
to quer...@googlegroups.com
Hi Timo,

Please see my comments below inline and Thanks!


On Monday, February 10, 2014 11:13:28 AM UTC-8, timowest wrote:
Hi.

On Friday, February 7, 2014 9:48:19 AM UTC+2, Jian Wu wrote:
Hi Timo,

I have two specific questions:

1. If we want to hide/prevent some Entity Properties associated with UserType from being used in query condition through querydsl, how can we change the codeine process? I can see, by default, querydsl has generated Q[UserType] through "EmbeddableSerializer" for each UserType

Could you provide an example of an entity and usertype with the top level annotations?


We are using Hibernate mapping as we want to clearly separate middle-tier java and back-end db schema; for example, a hibernate class "Entity" has an "internalattr" property with "InternalAttrUserType" as:

<hibernate-mapping>
<class name="Entity">
<property name="internalattr" type="InternalAttrUserType" access="field" />
</class>
</hibernate-mapping>

In App Context, "internalattr" is a "non-queryable" property, if we plan to develop a special "DomainExporter", is it possible that we can control the codegen to skip certain property or UserType in the generated "QEntity" class?    
 
Normally user types should be treated differently.

2. How set parameter associated with UserType through querydsl? will querydsl create CustomType from UserType internally?

Do you mean a parameter with the type of a usertype or something else?

 Yes, as I understand, I can use querydsl "ParamExpression" to set parameter, I just want to double-check that "ParamExpression" will work with UserType parameter since I can not find an example for that.

timowest

unread,
Feb 11, 2014, 2:08:56 PM2/11/14
to quer...@googlegroups.com
Hi.


On Tuesday, February 11, 2014 7:47:50 AM UTC+2, Jian Wu wrote:
Hi Timo,

Please see my comments below inline and Thanks!


On Monday, February 10, 2014 11:13:28 AM UTC-8, timowest wrote:
Hi.

On Friday, February 7, 2014 9:48:19 AM UTC+2, Jian Wu wrote:
Hi Timo,

I have two specific questions:

1. If we want to hide/prevent some Entity Properties associated with UserType from being used in query condition through querydsl, how can we change the codeine process? I can see, by default, querydsl has generated Q[UserType] through "EmbeddableSerializer" for each UserType

Could you provide an example of an entity and usertype with the top level annotations?


We are using Hibernate mapping as we want to clearly separate middle-tier java and back-end db schema; for example, a hibernate class "Entity" has an "internalattr" property with "InternalAttrUserType" as:

<hibernate-mapping>
<class name="Entity">
<property name="internalattr" type="InternalAttrUserType" access="field" />
</class>
</hibernate-mapping>

In App Context, "internalattr" is a "non-queryable" property, if we plan to develop a special "DomainExporter", is it possible that we can control the codegen to skip certain property or UserType in the generated "QEntity" class?    

Which tool do you use for code generation? Usertypes shouldn't be mapped to Q-types.
 
 
Normally user types should be treated differently.

2. How set parameter associated with UserType through querydsl? will querydsl create CustomType from UserType internally?

Do you mean a parameter with the type of a usertype or something else?

 Yes, as I understand, I can use querydsl "ParamExpression" to set parameter, I just want to double-check that "ParamExpression" will work with UserType parameter since I can not find an example for that.

ParamExpressions will work with user types.

Timo
 

Jian Wu

unread,
Feb 12, 2014, 12:36:17 AM2/12/14
to quer...@googlegroups.com


On Tuesday, February 11, 2014 11:08:56 AM UTC-8, timowest wrote:
Hi.

On Tuesday, February 11, 2014 7:47:50 AM UTC+2, Jian Wu wrote:
Hi Timo,

Please see my comments below inline and Thanks!


On Monday, February 10, 2014 11:13:28 AM UTC-8, timowest wrote:
Hi.

On Friday, February 7, 2014 9:48:19 AM UTC+2, Jian Wu wrote:
Hi Timo,

I have two specific questions:

1. If we want to hide/prevent some Entity Properties associated with UserType from being used in query condition through querydsl, how can we change the codeine process? I can see, by default, querydsl has generated Q[UserType] through "EmbeddableSerializer" for each UserType

Could you provide an example of an entity and usertype with the top level annotations?


We are using Hibernate mapping as we want to clearly separate middle-tier java and back-end db schema; for example, a hibernate class "Entity" has an "internalattr" property with "InternalAttrUserType" as:

<hibernate-mapping>
<class name="Entity">
<property name="internalattr" type="InternalAttrUserType" access="field" />
</class>
</hibernate-mapping>

In App Context, "internalattr" is a "non-queryable" property, if we plan to develop a special "DomainExporter", is it possible that we can control the codegen to skip certain property or UserType in the generated "QEntity" class?    

Which tool do you use for code generation? Usertypes shouldn't be mapped to Q-types.

I'm using HibernateDomainExporter, I can see a "QInternalAttr" type is generated, and it is referenced  in "QEntity" as:
      public QEntity extends EntityPathBase<Entity> {
             public final QInternalAttr internalattr = new QInternalAttr( forProperty("internalattr"));
   }

Is it possible that the "QInternalAttr internalattr" in "QEntity" can be skipped or commented out through a special "Domain Exporter"?

 
 
Normally user types should be treated differently.

2. How set parameter associated with UserType through querydsl? will querydsl create CustomType from UserType internally?

Do you mean a parameter with the type of a usertype or something else?

 Yes, as I understand, I can use querydsl "ParamExpression" to set parameter, I just want to double-check that "ParamExpression" will work with UserType parameter since I can not find an example for that.

ParamExpressions will work with user types.

Thanks a lot for the info!

timowest

unread,
Feb 13, 2014, 3:54:32 PM2/13/14
to quer...@googlegroups.com
Hi.

I just fixed this faulty behaviour. Here are the related tickets

https://github.com/mysema/querydsl/issues/665
https://github.com/mysema/querydsl/issues/664

The fix is available in the latest SNAPSHOT from here https://oss.sonatype.org/content/repositories/snapshots/

Br,
Timo

Jian Wu

unread,
Feb 14, 2014, 2:38:37 AM2/14/14
to quer...@googlegroups.com
Hi Timo,

Thanks a lot for your effort!

I just went through github issue 664 & 665, if I understand correctly, by annotating member variables with "@QueryType" in Java Entity class, HibernateDomainExporter will skip non-annotated member variable. I'll give a try with the snapshot repository.

Thanks again for your help!

Jian  

timowest

unread,
Feb 17, 2014, 2:23:21 PM2/17/14
to quer...@googlegroups.com
Hi.

Did you try it? @QueryType annotation usage should not be necessary in your case. The default is now that for unknown classes no Q-types are generated.

Timo

Jian Wu

unread,
Feb 19, 2014, 2:02:32 AM2/19/14
to quer...@googlegroups.com
Hi Timo,

I created a sample project (https://github.com/jianwu-github/querydsl-hbm-usertype-example) to test this new feature; the following are what I did:

1. I cloned the latest querydsl from github as I can see your fixes (issue 664 and 665) from history, I built and installed querydsl 3.3.2.BUILD-SNAPSHOT to my local maven repository.
2. My sample project "querydsl-hbm-usertype-example" actually is a Maven Multi-Module Project, which has:
    -- querydsl-hbm-plugin: a simple maven plugin calling HibernateDomainExporter to generate Querydsl class(es):
       ====================================================================================
         HibernateDomainExporter theExporter = new HibernateDomainExporter(prefix, targetFolder, theConfiguration);
         theExporter.setUnknownAsEntity(false);
         theExporter.execute();
       ====================================================================================

    -- hibernate-entities: a simple Hibernate Entity "Subscriber" mapped postgres db table "subscriber" through "Subscriber.hbm.xml"
       ====================================================================================
          public class Subscriber {
              @QueryType(PropertyType.SIMPLE)
               private FacebookId facebookId;

               private InternalAttr internalAttr;
          }
       ====================================================================================

    -- querydsl-codegen: using querydsl-hbm-plugin to generate "QSubscriber" with a simple UserTypeTest
       ====================================================================================
       querydsl-codegent/target/generate-sources/.../QSubscriber.java:

       /**
        * QSubscriber is a Querydsl query type for Subscriber
        */
      @Generated("com.mysema.query.codegen.EntitySerializer")
      public class QSubscriber extends EntityPathBase<Subscriber> {

           private static final long serialVersionUID = -2054050710L;

           public static final QSubscriber subscriber = new QSubscriber("subscriber");

           public final NumberPath<Integer> age = createNumber("age", Integer.class);

           public final SimplePath<com.querydslexample.hibernate.usertype.FacebookId> facebookId = createSimple("facebookId", com.querydslexample.hibernate.usertype.FacebookId.class);

           public final NumberPath<Long> id = createNumber("id", Long.class);

           public final SimplePath<com.querydslexample.hibernate.usertype.InternalAttr> internalAttr = createSimple("internalAttr", com.querydslexample.hibernate.usertype.InternalAttr.class);

           public final StringPath name = createString("name");

           public QSubscriber(String variable) {
               super(Subscriber.class, forVariable(variable));
           }

           public QSubscriber(Path<? extends Subscriber> path) {
              super(path.getType(), path.getMetadata());
           }

           public QSubscriber(PathMetadata<?> metadata) {
              super(Subscriber.class, metadata);
           }

        }
       ====================================================================================

        It looks like that HibernateDomainExporter did not skip the un-annotated "internalAttr". It would be great that you can take a look at my sample project on github  (https://github.com/jianwu-github/querydsl-hbm-usertype-example) .

        You need create a local postgresql db "querydslexample" and run "postgres_subscriber_table.sql" either using psql or pgAdmin to create "subscriber" table.

        
Thanks in advance!

Jian

timowest

unread,
Feb 19, 2014, 11:30:42 AM2/19/14
to quer...@googlegroups.com
Hi.

The default for unknown types is SimplePath, if you need full skipping you will need to to use QueryType(PropertyType.NONE)

Timo

Jian Wu

unread,
Feb 19, 2014, 10:35:36 PM2/19/14
to quer...@googlegroups.com
Hi Timo,

You are right, I got the correct "QSubscriber" Query Type after using "@QueryType(PropertyType.NONE)", and I already updated my sample project.

Thanks again for your instant help!

Jian
Reply all
Reply to author
Forward
0 new messages