Spring数据 - 规范加入层次结构

时间:2017-09-14 02:14:54

标签: java hibernate spring-data

我的规范有问题,我试图在两个类之间进行左连接,这是两个抽象类的子节点。

我有一个像这样的层次结构:

@Entity
@Table(name = "product")
public abstract class Product {
  @Id
  @SequenceGenerator(name = "product_seq", sequenceName = "product_seq", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "product_seq")
  @Column(updatable = false)
  private Long id;
  private String name;
  private String brand;
}

@Entity
@Table(name = "product")
public class Fridge extends Product {

  @Id
  @SequenceGenerator(name = "product_seq", sequenceName = "product_seq", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "product_seq")
  @Column(updatable = false)
  private Long id;

  private String otherField;
  ...
}

另外,我还有其他类这样的课程:

@Entity
@Table(name = "offer")
public abstract class Offer {

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name="PRODUCT_ID")
  private Product product;
  ...
}

@Entity
@JsonDeserialize(using = FridgeOfferDeserializer.class)
@Table(name = "offer")
public class FridgeOffer extends Offer {

  @Id
  @SequenceGenerator(name = "offer_gen", sequenceName = "offer_seq", allocationSize = 1)
  @GeneratedValue(strategy= GenerationType.IDENTITY, generator = "offer_gen")
  private Long id;

  @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name="PRODUCT_ID")
  private Fridge product;
  ...
}

在我的规范中,我有一个这样的代码:

@Override
public Predicate toPredicate(Root<FridgeOffer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

    Join<FridgeOffer, Fridge> join = root.join("product", JoinType.LEFT);
    Path<String> expression = join.get(searchCriteria.getKey());
    ...
}

但是每次我对孩子的字段执行请求过滤时,都会出现以下错误:

Unable to locate Attribute  with the the given name [otherField] on this ManagedType [com.fishingoffers.main.abstracts.Product]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [otherField] on this ManagedType [com.fishingoffers.main.abstracts.Product]

我尝试使用other_field(列的名称),但我遇到了同样的问题。消息中最重要的部分是,hibernate似乎是在Product类中进行查找,而不是我在规范中选择的Fridge。如果我尝试使用Product中定义的列,一切正常。我究竟做错了什么?

0 个答案:

没有答案