我遇到以下错误,但无法弄清楚导致问题的原因。
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']
资产载体实体:
@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
}
资产车辆库:
@Repository
public interface AssetVehicleRepository extends JpaRepository<AssetVehicle, UUID>, JpaSpecificationExecutor<AssetVehicle> {
Boolean existsByCompanyAndVehicleNumber(Company company, String vehicleNumber);
Optional<AssetVehicle> getByVehicleNumber(String vehicleNumber);
}
资产车辆服务:
@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));
}
}
我是 Spring Boot 开发的新手,有移动开发(Android、iOS、Flutter)背景。我已经被这个问题困扰了大约两天,但还没有找到解决方案。
我尝试重命名实体和表,但这并没有解决问题。
以前,我的资产只有一个表结构。我意识到随着数据库的增长,单个表可能会影响性能,因此我决定将资产类型分成单独的表,例如车辆、power_tool 和发电机。目前,我只有车辆类型。
PropertyReferenceException
是因为 Spring Data JPA 试图引用实体类 (AssetVehicle) 中的属性 (asset),但该属性在那里不存在。存储库方法名称应与实体类中的字段相对应。