弹簧数据中的实体关联问题

时间:2015-12-07 11:21:17

标签: spring jpa spring-data spring-data-jpa jpql

我有三个实体<div class="modal" role="dialog" tabindex="-1" data-type="Modal Default"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header" data-ng-class="modalOptions.headerClass" ng-show="title"> <button class="close" ng-click="modalOptions.close();" type="button">&times;</button> <h4 class="modal-title" ng-bind="modalOptions.headerTitle"></h4> </div> <div class="modal-body" ng-include="modalOptions.contentTemplate"></div> <div class="modal-footer"> <button class="btn btn-default" data-ng-click="modalOptions.close();" data-ng-show="modalOptions.showCloseButton" type="button"> {{modalOptions.closeButtonText}}</button> <button class="btn btn-primary" data-ng-click="modalOptions.ok();" data-ng-show="modalOptions.showActionButton"> {{modalOptions.actionButtonText}}</button> </div> </div> </div> </div> DrmAdvertisementDrmCategoryAdvertisement。现在我想在我的存储库中执行以下数据库查询,

DrmCategory

所以我创建了SELECT a.* FROM drm_advertisement a JOIN drm_category_advertisement ac ON ac.adid = a.adid where a.adtype = 'National' and ac.categoryid = 2; 的存储库,如下所示,

DrmAdvertisement

但是我们知道JPQL连接依赖于实体之间的关联,而不是ON子句。那么如何使用ON子句编写查询?

我的代码中是否遗漏了其他内容?我收到以下错误,

public interface AdvertisementRepository extends
        CrudRepository<DrmAdvertisement, Long> {

 @Query("SELECT a FROM DrmAdvertisement a join DrmCategoryAdvertisement ac ON ac.adid = a.adid where a.adType = :adType and ac.categoryid = :categoryid")
    public List<DrmAdvertisement> findAdByCategory(@Param("adType") String adType, @Param("categoryid") Long categoryid);


}

我的实体类是,

DrmAdvertisement:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT a FROM com.dooreme.domain.DrmAdvertisement a join DrmCategoryAdvertisement ac ON ac.adid = a.adid where a.adType = :adType and ac.categoryid = :categoryid]
    org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1679)
    org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
    org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
    org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

DrmCategory

@Entity
@Table(name = "drm_advertisement")
public class DrmAdvertisement implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long adId;

    @Column(nullable = false)
    private UUID uuid;

    @Column(nullable = true)
    private String adName;

    @Column(nullable = true)
    private String adDetails;

    @Column(nullable = true)
    private String adType;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "drm_category_advertisement", joinColumns = @JoinColumn(name = "adid"), inverseJoinColumns = @JoinColumn(name = "categoryId"))
    private DrmCategory drmCategory;
}

DrmCategoryAdvertisement

@Entity
@Table(name = "drm_category")
@NamedQuery(name = "DrmCategory.findAll", query = "SELECT d FROM DrmCategory d")
public class DrmCategory implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private long categoryId;

    @Column(nullable = false)
    private UUID uuid;

    @Column(nullable = true)
    private String categoryName;

    @Column(nullable = false)
    private long parentId;

    @Column(nullable = true)
    private String path;

    @Column(nullable = true)
    private Date createDate;

    @Column(nullable = true)
    private Date modifiedDate;

    @Column(nullable = true)
    private boolean active;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "drm_category_advertisement", joinColumns = @JoinColumn(name = "categoryId"), inverseJoinColumns = @JoinColumn(name = "adid"))
    private Set<DrmAdvertisement> drmAdvertisementSet;

}

CatAdvId

@Entity
@Table(name = "drm_category_advertisement")
@NamedQuery(name = "DrmCategoryAdvertisement.findAll", query = "SELECT d FROM DrmCategoryAdvertisement d")
public class DrmCategoryAdvertisement implements Serializable{

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private CatAdvId catAdvId;

     // bi-directional many-to-one association to DrmAdvertisement
    @OneToOne
    @JoinColumn(name = "adId", referencedColumnName = "adid", insertable = false, updatable = false)
    private DrmAdvertisement drmAdvertisement;

    // bi-directional many-to-one association to DrmSubscriber
    @OneToOne
    @JoinColumn(name = "categoryId", referencedColumnName = "categoryid", insertable = false, updatable = false)
    private DrmCategory drmCategory;
}

1 个答案:

答案 0 :(得分:1)

您必须映射DrmAdvertisement和DrmCategoryAdvertisement之间的关系,然后您的查询将是:

SELECT a FROM DrmAdvertisement a where a.adType = :adType and a.drmCategory.categoryId = :categoryid

编辑:

您可以删除映射DrmCategoryAdvertisement和CatAdvId,并使用&#39; mappedBy&#39;在DrmCategory:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "drmCategory")
private Set<DrmAdvertisement> drmAdvertisementSet;