Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querydsl @QueryInit not working

Tags:

querydsl

I'm working on a project I've recently taken over and am attempting to convert to use Querydsl.

I have a Listing class:

@Entity
public class EListing {

...

   private static final long serialVersionUID = 7729681308228522567L;

   @QueryInit("provider")
   private EExternalId masterId;

   @OneToOne(fetch = FetchType.EAGER,
             orphanRemoval = true,
             cascade = CascadeType.ALL)
   @JoinColumn(name = "master_ext_id")
   @NotNull(message = EvalMessageCode.LISTING_MASTERID_NOTNULL)
   @Valid
   public EExternalId getMasterId() {

      return masterId;
   }

   public void setMasterId(final EExternalId masterId) {

      this.masterId = masterId;
   }

... 

}

And an External ID class:

@Entity
public class EExternalId implements Serializable {

   public static final String FIELD_PROVIDER_PREFIX = "provder.";

   private static final long serialVersionUID = -4577890108032056848L;

   private String externalId;

   private Long id;

   private EDataProvider provider;

   @Column(name = "external_id",
           updatable = false)
   @NotBlank(message = EvalMessageCode.EXTID_ID_NOTBLANK)
   public String getExternalId() {

      return externalId;
   }

   @Id
   @GeneratedValue(generator = "external_id_seq")
   @Column(name = "id")
   public Long getId() {

      return id;
   }

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "provider_id",
               nullable = false,
               updatable = false)
   @IndexedEmbedded(prefix = FIELD_PROVIDER_PREFIX)
   @NotNull(message = EvalMessageCode.EXTID_PROVIDER_NOTNULL)
   public EDataProvider getProvider() {

      return provider;
   }

   public void setExternalId(final String externalId) {

      this.externalId = externalId;
   }

   public void setId(final Long id) {

      this.id = id;
   }

   public void setProvider(final EDataProvider provider) {

      this.provider = provider;
   }

}

I have a ListingPredicates class:

public class ListingPredicates {

   public static BooleanExpression byMasterExternalIdAndProviderId(
         final String externalId, final Long providerId) {

      QEListing listing = QEListing.eListing;

      return listing.masterId.externalId.equalsIgnoreCase(externalId).and(
            listing.masterId.provider.id.eq(providerId));
   }
}

When I attempt to access listing.masterId.provider in ListingPredicates.byMasterExternalIdAndProviderId(), provider is null, despite the fact that I've used QueryInit().

If I debug the creation of the Querydsl "Q" classes, there are no init paths for the masterId.

Can someone please explain why my @QueryInit isn't working?

Updates:

QEListing class:

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

    private static final long serialVersionUID = -1539977778;

    private static final PathInits INITS = PathInits.DIRECT;

    public static final QEListing eListing = new QEListing("eListing");

    public final com.mycompany.core.model.account.QEAccount account;

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

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

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

    public final com.mycompany.core.model.location.QECity city;

    public final SetPath<com.mycompany.core.model.listing.contact.EListingContact, com.mycompany.core.model.listing.contact.QEListingContact> contactInfo = this.<com.mycompany.core.model.listing.contact.EListingContact, com.mycompany.core.model.listing.contact.QEListingContact>createSet("contactInfo", com.mycompany.core.model.listing.contact.EListingContact.class, com.mycompany.core.model.listing.contact.QEListingContact.class, PathInits.DIRECT);

    public final DateTimePath<java.util.Calendar> createdDate = createDateTime("createdDate", java.util.Calendar.class);

    public final SetPath<com.mycompany.core.model.listing.contact.EEnhancedInfo, com.mycompany.core.model.listing.contact.QEEnhancedInfo> enhancedInfo = this.<com.mycompany.core.model.listing.contact.EEnhancedInfo, com.mycompany.core.model.listing.contact.QEEnhancedInfo>createSet("enhancedInfo", com.mycompany.core.model.listing.contact.EEnhancedInfo.class, com.mycompany.core.model.listing.contact.QEEnhancedInfo.class, PathInits.DIRECT);

    public final SetPath<EExternalId, QEExternalId> externalIds = this.<EExternalId, QEExternalId>createSet("externalIds", EExternalId.class, QEExternalId.class, PathInits.DIRECT);

    public final SetPath<com.mycompany.core.model.heading.EHeading, com.mycompany.core.model.heading.QEHeading> headings = this.<com.mycompany.core.model.heading.EHeading, com.mycompany.core.model.heading.QEHeading>createSet("headings", com.mycompany.core.model.heading.EHeading.class, com.mycompany.core.model.heading.QEHeading.class, PathInits.DIRECT);

    public final SetPath<EHoursOfOperation, QEHoursOfOperation> hoursOfOperations = this.<EHoursOfOperation, QEHoursOfOperation>createSet("hoursOfOperations", EHoursOfOperation.class, QEHoursOfOperation.class, PathInits.DIRECT);

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

    public final NumberPath<java.math.BigDecimal> latitude = createNumber("latitude", java.math.BigDecimal.class);

    public final NumberPath<java.math.BigDecimal> longitude = createNumber("longitude", java.math.BigDecimal.class);

    public final QEExternalId masterId;

    public final DateTimePath<java.util.Calendar> modifiedDate = createDateTime("modifiedDate", java.util.Calendar.class);

    public final SetPath<EDataProvider, QEDataProvider> removeForProviders = this.<EDataProvider, QEDataProvider>createSet("removeForProviders", EDataProvider.class, QEDataProvider.class, PathInits.DIRECT);

    public final SetPath<ESponsorship, QESponsorship> sponsorships = this.<ESponsorship, QESponsorship>createSet("sponsorships", ESponsorship.class, QESponsorship.class, PathInits.DIRECT);

    public final com.mycompany.core.model.location.QEState state;

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

    public final com.mycompany.core.model.location.QEZipCode zipCode;

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

    public QEListing(String variable) {
        this(EListing.class, forVariable(variable), INITS);
    }

    @SuppressWarnings("all")
    public QEListing(Path<? extends EListing> path) {
        this((Class)path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QEListing(PathMetadata<?> metadata) {
        this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QEListing(PathMetadata<?> metadata, PathInits inits) {
        this(EListing.class, metadata, inits);
    }

    public QEListing(Class<? extends EListing> type, PathMetadata<?> metadata, PathInits inits) {
        super(type, metadata, inits);
        this.account = inits.isInitialized("account") ? new com.mycompany.core.model.account.QEAccount(forProperty("account"), inits.get("account")) : null;
        this.city = inits.isInitialized("city") ? new com.mycompany.core.model.location.QECity(forProperty("city"), inits.get("city")) : null;
        this.masterId = inits.isInitialized("masterId") ? new QEExternalId(forProperty("masterId"), inits.get("masterId")) : null;
        this.state = inits.isInitialized("state") ? new com.mycompany.core.model.location.QEState(forProperty("state")) : null;
        this.zipCode = inits.isInitialized("zipCode") ? new com.mycompany.core.model.location.QEZipCode(forProperty("zipCode"), inits.get("zipCode")) : null;
    }

}  

Pom config for maven apt plugin:

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>maven-apt-plugin</artifactId>
    <version>1.0.4</version>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources</outputDirectory>
                <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                <!-- <logOnlyOnError>true</logOnlyOnError> -->
            </configuration>
        </execution>
    </executions>
</plugin>
like image 416
Luke Avatar asked May 29 '13 21:05

Luke


1 Answers

The Querydsl JPA annotation processor decides on a class basis if fields, accessors or both are taken into account in the inspection, based on where relevant annotations are used. Only JPA annotations are considered in this phase.

Since your JPA annotations are defined in the getters, the QueryInit annotation needs to be put there as well.

like image 71
Timo Westkämper Avatar answered Jan 02 '23 23:01

Timo Westkämper