1

I use spring data, jpa with hibernate implementation

I am writing a advanced search

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;
    ...
}

and this search method

 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<Samplings, 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);
}

For my supplier field search, how I suppose to do the join GranulateSamplings who extend product? because this field is available only in GranulateSamplings entity

Edit

I get this error

Unable to locate Attribute with the give name [suppliers] on this Managedtype com.lcm.mode.Samplings

It's like is not able to do alone the bridge between samplings and suppliers

Edit

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()));

same issue

robert trudel
  • 5,283
  • 17
  • 72
  • 124

1 Answers1

0
Join<Products, suppliers> supp = product.join("suppliers");
p.getExpressions().add(cb.equal(supp.get("id"), search.getSuppliersId()));

SuppliersId indicated that you have more than one, just iterate and add all the equals that u need

PD: Classes, especially entities, shouldn't be named in plural

FiruzzZ
  • 661
  • 1
  • 6
  • 21
  • Suppliers is not directly in product but GranulateProducts... jpa will be able to find it? easy to find people who said the opposite about plural... anyway, nothing to do with the initial post – robert trudel Jul 18 '18 at 20:05
  • right, if you are going to filter by some `suppliersId`, the join should be of the Granulate not the generic type `Product` `Join` – FiruzzZ Jul 18 '18 at 22:46