Я изучаю 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
Spring JPA: объект со встроенным ключом не удаляется ⇐ JAVA
Программисты JAVA общаются здесь
-
Anonymous
1728036259
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\
Подробнее здесь: [url]https://stackoverflow.com/questions/78995965/spring-jpa-entity-with-embedded-key-is-not-deleted[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия