无法在任何字段上获取

时间:2018-07-18 18:55:11

标签: hibernate jpa spring-data-jpa

我使用带有jpa和hibernate的spring boot

@Entity
@IdClass(SamplesPK.class)
public class Samples{
    ..
    @Id
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name = "sampling_id", referencedColumnName = "id"),
        @JoinColumn(name = "sampling_year", referencedColumnName = "year")})
    private Samplings sampling;
    ...
}

@Entity
@IdClass(SamplingsPK.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {
    @OneToOne
    private Products product;
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Products{
    ...
    @ManyToOne(fetch = FetchType.LAZY)
    private ProductTypes productType;
}

@Entity
@DiscriminatorValue("TraditionalProducts")
public class TraditionalProducts extends Products {
    ...
}

@Entity
@DiscriminatorValue("GranulateProducts")
public class GranulateProducts extends Products  {
     @ManyToMany(mappedBy="granulateProducts")
    private Set<Suppliers> suppliers = new HashSet<>();
}

在存储库中,我编写了此查询

 @Query(value = "select s from Samples s Join Fetch s.sampling sp Join Fetch sp.machine m Join Fetch sp.product p Join Fetch p.productType")
 public Page<Samples> findAllFullSample(Pageable pageable);

我收到此消息

  

由于:java.lang.IllegalArgumentException:   org.hibernate.QueryException:查询指定的联接获取,但是   选择列表中不存在所提取关联的所有者   [FromElement {显式,不是集合连接,获取连接,获取非延迟   属性,classAlias = sp,role = com.lcm.model.Samples.sampling,tableName =样本,tableAlias = samplings1_,origin = samples   samples0_,columns = {samples0_.sampling_id samples0_.sampling_year   ,className = com.lcm.model.Samplings}}] [从以下列表中选择计数   com.lcm.model.Samples的内部联接获取s.sampling sp的内部联接   提取sp.machine m内部联接提取sp.product p内部联接提取   p.productType]

     

由以下原因引起:org.hibernate.QueryException:查询指定的联接   正在抓取,但抓取的关联的所有者不在   选择列表[FromElement {显式,不是集合联接,获取   加入,获取非延迟   属性,classAlias = sp,role = com.lcm.model.Samples.sampling,tableName =样本,tableAlias = samplings1_,origin = samples   samples0_,columns = {samples0_.sampling_id samples0_.sampling_year   ,className = com.lcm.model.Samplings}}] [从以下列表中选择计数   com.lcm.model.Samples的内部联接获取s.sampling sp的内部联接   提取sp.machine m内部联接提取sp.product p内部联接提取   p.productType]

如果我删除了所有提取命令,它就可以正常工作,为什么我不能使用提取?

1 个答案:

答案 0 :(得分:1)

您可以在分页中使用访存,但是您需要对计数行进行查询...

检查此帖子 https://codingexplained.com/coding/java/spring-framework/fetch-query-not-working-spring-data-jpa-pageable#comment-293535