Spring JPA: объект со встроенным ключом не удаляетсяJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Spring JPA: объект со встроенным ключом не удаляется

Сообщение Anonymous »

Я изучаю Spring Boot, и до сих пор все было хорошо, используя Spring JPA для управления базой данных, пока я не попытался удалить объект, имеющий встроенный ключ.
Соответствующий объект выглядит следующим образом:
@Setter
@Getter
@Data
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Cacheable
@ToString
@Builder
@Table(name = "test_case_execution_result")
@NamedQueries({
@NamedQuery(
name = "TestCaseExecutionResult.findAll",
query = "SELECT t FROM TestCaseExecutionResult t"),
@NamedQuery(
name = "TestCaseExecutionResult.findById",
query = "SELECT t FROM TestCaseExecutionResult t WHERE t.testCaseExecutionResultPK.id = :id"),
@NamedQuery(
name = "TestCaseExecutionResult.findByTestCaseResultId",
query =
"SELECT t FROM TestCaseExecutionResult t WHERE"
\+ " t.testCaseExecutionResultPK.testCaseResultId = :testCaseResultId"),
@NamedQuery(
name = "TestCaseExecutionResult.findByTestCaseId",
query =
"SELECT t FROM TestCaseExecutionResult t WHERE t.testCaseExecutionResultPK.testCaseId ="
\+ " :testCaseId"),
@NamedQuery(
name = "TestCaseExecutionResult.findByTestRegionId",
query =
"SELECT t FROM TestCaseExecutionResult t WHERE t.testCaseExecutionResultPK.testRegionId ="
\+ " :testRegionId"),
@NamedQuery(
name = "TestCaseExecutionResult.findByDate",
query = "SELECT t FROM TestCaseExecutionResult t WHERE t.date = :date"),
@NamedQuery(
name = "TestCaseExecutionResult.findByRelatedJob",
query = "SELECT t FROM TestCaseExecutionResult t WHERE t.relatedJob = :relatedJob")
})

public class TestCaseExecutionResult implements Serializable {

@Serial private static final long serialVersionUID = 1L;
@EmbeddedId protected TestCaseExecutionResultPK testCaseExecutionResultPK;

@Basic(optional = false)
@NotNull @Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
private Date date;

@Basic(optional = false)
@NotNull @Size(min = 1, max = 255)
@Column(name = "relatedJob")
private String relatedJob;

@JoinColumn(
name = "test_case_id",
referencedColumnName = "id",
insertable = false,
updatable = false)
@ManyToOne(optional = false)
private TestCase testCase;

@JoinColumn(
name = "test_case_result_id",
referencedColumnName = "id",
insertable = false,
updatable = false)
@ManyToOne(optional = false)
private TestCaseResult testCaseResult;

@JoinColumn(
name = "test_region_id",
referencedColumnName = "id",
insertable = false,
updatable = false)
@ManyToOne(optional = false)
private TestRegion testRegion;

public TestCaseExecutionResult(TestCaseExecutionResultPK testCaseExecutionResultPK) {
this.testCaseExecutionResultPK = testCaseExecutionResultPK;
}

public TestCaseExecutionResult(
TestCaseExecutionResultPK testCaseExecutionResultPK, Date date, String relatedJob) {
this.testCaseExecutionResultPK = testCaseExecutionResultPK;
this.date = date;
this.relatedJob = relatedJob;
}

public TestCaseExecutionResult(
int testCaseExecutionResultPK, int testCaseResultId, int testCaseId, int testRegionId) {
this.testCaseExecutionResultPK =
new TestCaseExecutionResultPK(
testCaseExecutionResultPK, testCaseResultId, testCaseId, testRegionId);
}

@Override
public int hashCode() {
int hash = 0;
hash += (testCaseExecutionResultPK != null ? testCaseExecutionResultPK.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof TestCaseExecutionResult other)) {
return false;
}
return (this.testCaseExecutionResultPK != null || other.testCaseExecutionResultPK == null)
&& (this.testCaseExecutionResultPK == null
|| this.testCaseExecutionResultPK.equals(other.testCaseExecutionResultPK));
}
}

Это соответствующий контроллер:
@RestController
@SuppressWarnings("PMD.TestClassWithoutTestCases")
public class TestCaseExecutionResultController {

private final TestCaseExecutionResultRepository repository;
private final TestCaseExecutionResultAssembler assembler;

public TestCaseExecutionResultController(
TestCaseExecutionResultRepository repository, TestCaseExecutionResultAssembler assembler) {
this.repository = repository;
this.assembler = assembler;
}

@PostMapping(Endpoints.EXECUTION_RESULT_REGION)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity\ createTestCaseExecutionResult(
@RequestBody TestCaseExecutionResult testCaseExecutionResult,
@PathVariable Integer tcId,
@PathVariable Integer regionId,
@PathVariable Integer resultId) {
if (testCaseExecutionResult.getTestCaseExecutionResultPK() == null) {
testCaseExecutionResult.setTestCaseExecutionResultPK(
TestCaseExecutionResultPK.builder()
.testCaseResultId(resultId)
.testCaseId(tcId)
.testRegionId(regionId)
.build());
}
testCaseExecutionResult.setDate(new Date());
return ResponseEntity.ok(this.assembler.toModel(this.repository.save(testCaseExecutionResult)));
}

@GetMapping(Endpoints.EXECUTION_RESULTS)
public ResponseEntity\ findAll() {
return ResponseEntity.ok(this.assembler.toCollectionModel(this.repository.findAll()));
}

@GetMapping(Endpoints.EXECUTION_RESULTS_REGION)
public ResponseEntity\
executionResultsForTestCaseInRegion(
@PathVariable Integer tcId, @PathVariable Integer regionId) throws NotFoundException {
return ResponseEntity.ok(
this.assembler.toCollectionModel(
this.repository.findByTestCaseIdAndTestRegionId(tcId, regionId)));
}

@DeleteMapping(Endpoints.EXECUTION_RESULT_REGION_EXECUTION)
public ResponseEntity\

Подробнее здесь: https://stackoverflow.com/questions/789 ... ot-deleted
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»