skip to Main Content

I’m getting below error and couldn’t figure it what causes the issue.

2024-10-19T11:26:28.546+05:30 WARN 9455 --- [rentsysapi] [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.data.mapping.PropertyReferenceException: No property 'asset' found for type 'AssetVehicle']

Asset Vehicle entity:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "asset_vehicle", schema = "public")
@EntityListeners(AuditingEntityListener.class)
@Audited
public class AssetVehicle extends BaseExpireEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @UuidGenerator
    @Column(updatable = false, nullable = false, columnDefinition = "UUID")
    private UUID assetVehicleId;

    @Enumerated(value = EnumType.STRING)
    @Column(nullable = false)
    private AssetStatus status = AssetStatus.AVAILABLE;

    private String vehicleNumber;
    private String vin;
    private String enginNumber;
    private String chassisNumber;
    private String color;

    /// Year of manufacture
    private Integer yom;

    /// Year of registration
    private Integer yor;

    @Column(columnDefinition = "TEXT")
    private String remark;

    @Column(nullable = false, updatable = false)
    private UUID primaryBranchId;

    //region relationships
    @ManyToOne
    @JoinColumn(name = "company_id", nullable = false, updatable = false)
    private Company company;

    @ManyToOne
    @JoinColumn(name = "owner_id")
    private UserEntity owner;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "vehicle_id", nullable = false)
    private Vehicle vehicle;

    @OneToMany(mappedBy = "assetVehicle", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private Set<AssetVehicleMedia> media = new HashSet<>();

    @OneToMany(mappedBy = "assetVehicle", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private Set<AssetVehicleDocument> documents = new HashSet<>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "asset_vehicle_branch",
            joinColumns = @JoinColumn(name = "asset_vehicle_id"),
            inverseJoinColumns = @JoinColumn(name = "branch_id")
    )
    private Set<Branch> branches = new HashSet<>();
    //endregion
}

Asset Vehicle Repository:

@Repository
public interface AssetVehicleRepository extends JpaRepository<AssetVehicle, UUID>, JpaSpecificationExecutor<AssetVehicle> {

    Boolean existsByCompanyAndVehicleNumber(Company company, String vehicleNumber);

    Optional<AssetVehicle> getByVehicleNumber(String vehicleNumber);
}

Asset Vehicle Service:

@Service
public class AssetVehicleServiceImpl implements AssetVehicleService {
    @Autowired
    CompanyService companyService;

    @Autowired
    AssetVehicleRepository assetVehicleRepository;
    @Autowired
    ModelMapper modelMapper;

    @Override
    public Boolean existsByCompanyVehicleNumber(UUID companyId, String vehicleNumber) {
        Company company = companyService.findById(companyId);
        return assetVehicleRepository.existsByCompanyAndVehicleNumber(company, vehicleNumber);
    }

    @Override
    public AssetVehicle getByVehicleNumber(String vehicleNumber) {
        return assetVehicleRepository.getByVehicleNumber(vehicleNumber)
                .orElseThrow(() -> new NotFoundException("Vehicle not found with vehicle number: " + vehicleNumber));
    }

    @Override
    public AssetVehicleDto getByVehicleNumberDtoDto(String vehicleNumber) {
        return modelMapper.map(getByVehicleNumber(vehicleNumber), AssetVehicleDto.class);
    }

    @Override
    public Page<AssetVehicle> getAllAssets(Pageable pageable, AssetStatus status, UUID companyId, UUID ownerId) {
        Specification<AssetVehicle> specification = Specification.where(AssetVehicleSpecification.hasStatus(status))
                .and(AssetVehicleSpecification.hasCompany(companyId))
                .and(AssetVehicleSpecification.hasOwner(ownerId));

        return assetVehicleRepository.findAll(specification, pageable);
    }

    @Override
    public Page<AssetVehicleDto> getAllAssetsDto(Pageable pageable, AssetStatus status, UUID companyId, UUID ownerId) {
        Page<AssetVehicle> assets = getAllAssets(pageable, status, companyId, ownerId);
        return assets.map(asset -> modelMapper.map(asset, AssetVehicleDto.class));
    }
}

I’m new to Spring Boot development, coming from a background in mobile development (Android, iOS, Flutter). I’ve been struggling with this issue for about two days and haven’t been able to find a solution yet.

I’ve tried renaming entities and tables, but that hasn’t resolved the problem.

Previously, I had a single table structure for assets. I realized that a single table could impact performance as the database grows, so I decided to separate asset types into individual tables, such as vehicle, power_tool, and generator. For now, I only have the vehicle type.

2

Answers


  1. Chosen as BEST ANSWER

    The cause for the issue is I have used "asset_vehicle_id" as my sorting property. But after a few research i found that property name should be same as entity property name not the sql column name.

    So below is my previous controller: asset_vehicle_id

    @GetMapping
    public BaseResponseDto<Page<AssetVehicleDto>> getAllAssetVehicles(
            @RequestParam(value = "page", defaultValue = "0") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "company_id", required = false) String companyId,
            @RequestParam(value = "owner_id", required = false) String ownerId,
            @RequestParam(value = "sort", defaultValue = "asset_vehicle_id") String sortField,
            @RequestParam(value = "direction", defaultValue = "ASC") String sortDirection
    ) {
        Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortField);
        Pageable pageable = PageRequest.of(page, size, sort);
        return BaseResponseDto.success(assetService.getAllAssetsDto(pageable,
                StringUtils.hasText(status) ? AssetStatus.valueOf(status) : null,
                StringUtils.hasText(companyId) ? UUID.fromString(companyId) : null,
                StringUtils.hasText(ownerId) ? UUID.fromString(ownerId) : null
        ));
    }
    

    And my fixed implementation is: assetVehicleId

    @GetMapping
    public BaseResponseDto<Page<AssetVehicleDto>> getAllAssetVehicles(
            @RequestParam(value = "page", defaultValue = "0") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "company_id", required = false) String companyId,
            @RequestParam(value = "owner_id", required = false) String ownerId,
            @RequestParam(value = "sort", defaultValue = "assetVehicleId") String sortField,
            @RequestParam(value = "direction", defaultValue = "ASC") String sortDirection
    ) {
        Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortField);
        Pageable pageable = PageRequest.of(page, size, sort);
        return BaseResponseDto.success(assetService.getAllAssetsDto(pageable,
                StringUtils.hasText(status) ? AssetStatus.valueOf(status) : null,
                StringUtils.hasText(companyId) ? UUID.fromString(companyId) : null,
                StringUtils.hasText(ownerId) ? UUID.fromString(ownerId) : null
        ));
    }
    

  2. PropertyReferenceException, occurs because Spring Data JPA is trying to reference a property (asset) in your entity class (AssetVehicle), but it doesn’t exist there. The repository method name should correspond to fields in your entity class.

    Verify that your database schema matches the updated entity structure, and ensure there are no leftover annotations or queries from the previous single table setup.

    Clean and rebuild your project to clear any cached classes

    Enable logging for Spring Data JPA to identify your issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search