1

I need to validate if the attribute acronym of binUse is equal to BinUseAcronym.FPAN.toString(). I need access binUse.acronym

ProductBin

@Id
@Column(name = "ID")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BIN_USE_ID", nullable = false)
private BinUse binUse;

BinUse

@Id
@Column
private Long id;
@Column
private String acronym;

My CriteriaQuery

ProductBin productBin = this.productBinRepository.findOne(new Specification<ProductBin>() {
    @Override
    public Predicate toPredicate(Root<ProductBin> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> listPredicate = new ArrayList<>();
        listPredicate.add(cb.equal(root.get("start"), bin.getStart()));
        listPredicate.add(cb.equal(root.get("end"), bin.getEnd()));
        listPredicate.add(cb.equal(
            root.get("BinUse_.acronym"), BinUseAcronym.FPAN.toString())
        );

        return cb.and(listPredicate.toArray(new Predicate[listPredicate.size()]));
    }
});

I try to add the class name before attribute but I keep receiving a exception:

InvalidDataAccessApiUsageException: Unable to locate Attribute  with the the given name [BinUse_.acronym] on this ManagedType [com.edenred.commons.domain.ProductBin]; 

How can I resolve this error?

lloiacono
  • 4,714
  • 2
  • 30
  • 46
Daniela Morais
  • 2,125
  • 7
  • 28
  • 49
  • 2
    What is `BinUse_.acronym` supposed to do? Is there a property named `BinUse_` in `ProductBin`? If you wanted to refer to the value of `ProductBin.binUse.acronym`, just use `root.join("binUse").get("acronym")` – crizzis Jun 19 '17 at 17:31
  • @crizzis Exactly – Daniela Morais Jun 19 '17 at 17:37
  • @crizzis What's the difference between ```root.join("binUse").get("acronym")``` and ```root.get("binUse").get("acronym")```? – Daniela Morais Jun 19 '17 at 17:41
  • There is little difference in this case. `join` just makes it explicit that what is referenced here is a relationship (as opposed to a simple) attribute. Think of it as equivalent to the JPQL: `SELECT p FROM ProductBin p JOIN p.binUse b WHERE b ...` vs `SELECT p FROM ProductBin p WHERE p.binUse ...` – crizzis Jun 19 '17 at 19:21

0 Answers0