1

hi guys i have Entity like this

@Builder
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(name = "vendors")
public class Vendor extends Audit implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "netsuite_id")
    @JsonProperty("netSuiteId")
    private Integer netSuiteId;

    @Column(name = "company_name")
    @JsonProperty("companyName")
    private String companyName;
}

and here i have specifications search like this

  @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(value = "pageIndex", required = false, defaultValue = "0") Integer pageIndex,
            @RequestParam(value = "vendorId", required = false) Integer vendorId,
            @RequestParam(value = "vendorName", required = false) String vendorName
    ) {
        try {
            String field = "createdAt";
            Sort sort = Sort.by(Sort.Direction.DESC, field);
            List<Specification<Vendor>> specificationList = new ArrayList<>();
            if (Utils.isNotNullAndIsNotZero(vendorId)) {
                specificationList.add(VendorSpecs.getVendorById(vendorId));
            }
            if (Utils.isNotNullAndIsNotEmpty(vendorName)) {
                specificationList.add(VendorSpecs.getVendorByName(vendorName));
            }

and here my specification search

  public static Specification<Vendor> getVendorByName(String vendorName) {
        String lowerName = "%" + vendorName.toLowerCase() + "%";
        return (root, query, criteriaBuilder) -> {
            Expression<String> expName = root.get("companyName").as(String.class);
            Predicate predicateName = criteriaBuilder.like(criteriaBuilder.lower(expName), lowerName);

            return criteriaBuilder.or(predicateName);
        };
    }

here my service

@Override
    public Page<Vendor> searchVendor(List<Specification<Vendor>> specificationList, Integer pageSize, Integer pageIndex, Sort sort) {
        if (specificationList.size() > 0) {
            Specification<Vendor> tickerSpecification = Utils.andSpecification(specificationList).orElseThrow(() -> new IllegalArgumentException("No criteria provided"));
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            return vendorRepository.findAll(tickerSpecification, pageable);
        }

but when i want to get the data i got error like this

"Unable to locate Attribute  with the the given name [company_name] on this ManagedType

how to fix this issue ?

Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31
adhi mf
  • 21
  • 3
  • plz add your request – Kalaiselvan Dec 06 '21 at 16:09
  • I'm having the same problem, first thing, try to access the attribute list of your entity, see[1], look for its name under ManagedType attribute set. If you found how to achieve this, please let me know; [1] https://stackoverflow.com/questions/13265094/generic-way-to-get-jpa-entity-version – João Carlos Dec 06 '21 at 19:56
  • Do any of these solutions help? https://stackoverflow.com/questions/49319468/spring-data-jpa-unable-to-locate-attribute-with-the-given-name – Daly Dec 12 '21 at 18:36

0 Answers0