我想更新:SampleShipmentId,sampleId,descriptionin mySampleShipmentDiscrepanciesEntity我使用的是Sping Boot ,Java 17,Hibernate,JPA,MapStruct和Lombok。错误消息:
org.springframework.orm.jpa.JpaSystemException:domain.sampleshipment.persistence.SampleShipmentEntity示例的标识符已从3更改为1;嵌套异常为org.hibernate.hibernateException:domain.sampleshipment.persistence.SampleShipmentEntity示例的标识符已从3更改为1]
@Service
@Transactional
@RequiredArgsConstructor
public class SampleShipmentDiscrepanciesUpdateUseCase {
private final SampleShipmentDiscrepanciesStore store;
private final DtoMapper mapper;
public SampleShipmentDiscrepanciesUpdateResponse execute(long id, SampleShipmentDiscrepanciesUpdateRequest updateRequest) {
SampleShipmentDiscrepanciesEntity sampleShipmentDiscrepanciesEntity = store.load(id, IncludeFilter.EXCLUDE);
mapper.fromUpdateRequest(updateRequest, sampleShipmentDiscrepanciesEntity);
sampleShipmentDiscrepanciesEntity = store.save(sampleShipmentDiscrepanciesEntity);
return mapper.toUpdateResponse(sampleShipmentDiscrepanciesEntity);
}
@Mapper(componentModel = "spring")
interface DtoMapper {
@Mapping(target = "sampleShipment.id", source = "sampleShipmentId")
@Mapping(target = "sample.id", source = "sampleId")
void fromUpdateRequest(SampleShipmentDiscrepanciesUpdateRequest updateRequest,
@MappingTarget SampleShipmentDiscrepanciesEntity sampleShipmentDiscrepanciesEntity);
@Mapping(source = "sampleShipmentDiscrepanciesEntity.sampleShipment.id", target = "sampleShipmentId")
@Mapping(source = "sampleShipmentDiscrepanciesEntity.sample.id", target = "sampleId")
SampleShipmentDiscrepanciesUpdateResponse toUpdateResponse(SampleShipmentDiscrepanciesEntity sampleShipmentDiscrepanciesEntity);
}
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SampleShipmentDiscrepanciesUpdateRequest {
private long sampleShipmentId;
private long sampleId;
private String description;
}
@Entity
@Table(name = "sample_shipment_discrepancies")
@Getter
@Setter
@ToString(onlyExplicitlyIncluded = true)
public class SampleShipmentDiscrepanciesEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@ToString.Include
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sample_shipment_id", nullable = false)
private SampleShipmentEntity sampleShipment; // it has id that I want to map
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sample_id")
private SampleEntity sample;// it has id that I want to map
@Column(name = "description", nullable = false)
@ToString.Include
private String description;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SampleShipmentDiscrepanciesUpdateResponse {
private long id;
private long sampleShipmentId;
private long sampleId;
private String description;
}
@PutMapping("/{id}")
public SampleShipmentDiscrepanciesUpdateResponse update(@PathVariable long id, @RequestBody SampleShipmentDiscrepanciesUpdateRequest updateRequest) {
return sampleShipmentDiscrepanciesUpdateUseCase.execute(id, updateRequest);
}
@Service
@RequiredArgsConstructor
@NoTransactional
class SampleShipmentDiscrepanciesStoreAdapter implements SampleShipmentDiscrepanciesStore {
private final SampleShipmentDiscrepanciesRepository repository;
@Override
public SampleShipmentDiscrepanciesEntity load(final long id, IncludeFilter deleted) {
Specification<SampleShipmentDiscrepanciesEntity> spec = SpecificationHelper.create(FilterBuilder.equal("id", id));
spec = SpecificationHelper.addDeletedFilter(spec, deleted);
return repository.findOne(spec).orElseThrow(SampleShipmentDiscrepanciesNotFoundException::new);
}
@Override
public SampleShipmentDiscrepanciesEntity save(final SampleShipmentDiscrepanciesEntity sampleShipmentDiscrepanciesEntity) {
return repository.save(sampleShipmentDiscrepanciesEntity);
}
@Repository
interface SampleShipmentDiscrepanciesRepository extends JpaRepository<SampleShipmentDiscrepanciesEntity, Long>,
JpaSpecificationExecutor<SampleShipmentDiscrepanciesEntity> {
}
}
1条答案
按热度按时间4sup72z81#
我重写了SampleShipmentDiscrepanciesUpdateUpdaterCase中的方法,直接设置它。我还是不明白有什么问题。以及为什么MapStruct不能自动正确Map它。