I use spring boot 2, postgresql with jpa and hibernate 5.2.17.
In my application I use inheritance.
public class Samples extends BaseEntity{
..
@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 extends extends BaseEntity{
@OneToOne
private Products product;
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Products extends BaseEntity{
...
@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<>();
}
@Entity
@DiscriminatorValue("GranulateSamplings")
public class GranulateSamplings extends Samplings{
OneToOne(optional = true, fetch = FetchType.EAGER)
@JoinColumn
private Suppliers supplier;
...
}
public Page<Samples> advancedSearch(SamplesSearchDto search, Pageable page) {
Specification<Samples> specification = (Root<Samples> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
Predicate p = cb.conjunction();
if (search.getProductsId() != null) {
Join<Samples, Samplings> samplings = (Join) root.join("sampling");
Join<Samples, Products> product = (Join) samplings.join("product");
p.getExpressions().add(cb.equal(product.get("id"), search.getProductsId()));
}
if (search.getSuppliersId() != null) {
Join<Samples, Samplings> samplings = (Join) root.join("sampling");
Join<GranulateSamplings, Suppliers> supp = samplings.join("supplier");
p.getExpressions().add(cb.equal(supp.get("id"), search.getSuppliersId()));
//supplier name field is used in GranulateSamplings
}
return p;
};
return this.findAll(specification, page);
}
I get this error
Unable to locate Attribute with the the given name [supplier] on this ManagedType [com.lcm.model.Samplings]
It’s like inheritence is not see.
Edit
I saw type and treat but I was not able to get it working yet