组合主键上的Spring数据和@EntityGraph

时间:2018-09-07 14:45:38

标签: java spring-data-jpa entitygraph

我有此代码:

@Table(name = "contact_supplier_function")
@Entity
public class ContactSupplierFunctionEntity implements Serializable {
    private static final long serialVersionUID = 3969992836064953905L;

    @EmbeddedId
    private ContactSupplierFunctionEntityPk contactSupplierFunctionId;

    public ContactSupplierFunctionEntityPk getContactSupplierFunctionId() {
        return contactSupplierFunctionId;
    }

    public void setContactSupplierFunctionId(ContactSupplierFunctionEntityPk contactSupplierFunctionId) {
        this.contactSupplierFunctionId = contactSupplierFunctionId;
    }
}
@Embeddable
public class ContactSupplierFunctionEntityPk implements Serializable {
    private static final long serialVersionUID = -1343896399377494969L;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id ", nullable = false)
    private SupplierEntity supplier;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "function_id", nullable = false)
    private FunctionEntity function;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contact_id", nullable = false)
    private ContactEntity contact;

    public SupplierEntity getSupplier() {
        return supplier;
    }

    public void setSupplier(SupplierEntity supplier) {
        this.supplier = supplier;
    }

    public FunctionEntity getFunction() {
        return function;
    }

    public void setFunction(FunctionEntity function) {
        this.function = function;
    }

    public ContactEntity getContact() {
        return contact;
    }

    public void setContact(ContactEntity contact) {
        this.contact = contact;
    }
}

@Repository
public interface ContactSupplierFunctionDAO extends CrudRepository<ContactSupplierFunctionEntity, ContactSupplierFunctionEntityPk> {
    @EntityGraph(attributePaths = { "contactSupplierFunctionId.function" })
    List<ContactSupplierFunctionEntity> findByContactSupplierFunctionIdContactIdAndContactSupplierFunctionIdSupplierIdIn(
            Long contactId, List<Long> supplierIds);
}

我想用@EntityGraph将我的复合主键的数据加载到我的存储库中,因为我不想将其放入Fetch.EAGER中。

但是我有这个错误:

  

引发异常[请求处理失败;嵌套的异常是org.springframework.dao.InvalidDataAccessApiUsageException:属性[contactSupplierFunctionId]不是托管类型。嵌套异常是java.lang.IllegalArgumentException:具有根本原因的属性[contactSupplierFunctionId]不是托管类型]   java.lang.IllegalArgumentException:属性[contactSupplierFunctionId]不是托管类型

是否可以使用@EntityGraph做到这一点?

您还有其他解决方案吗?

谢谢

0 个答案:

没有答案