Querydsl @QueryInit无法正常工作

时间:2013-05-29 21:21:20

标签: querydsl

我正在开展一项我最近接手并试图转换为使用Querydsl的项目。

我有一个Listing类:

@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;
   }

... 

}

外部ID类:

@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;
   }

}

我有一个ListingPredicates类:

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));
   }
}

当我尝试访问ListingPredicates.byMasterExternalIdAndProviderId()中的listing.masterId.provider时,提供程序为空,尽管事实上我使用了QueryInit()

如果我调试Querydsl" Q"类,masterId没有初始化路径。

有人可以解释为什么@QueryInit无法正常工作吗?

更新

QEListing课程:

/**
 * 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;
    }

}  

用于maven apt插件的Pom配置:

<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>

1 个答案:

答案 0 :(得分:8)

Querydsl JPA注释处理器根据使用相关注释的位置,在检查中考虑字段,访问者或两者时决定类。在此阶段仅考虑JPA注释。

由于您的JPA注释是在getter中定义的,因此需要将QueryInit注释放在那里。