由於某種原因,我一直無法找到合適的答案。我有以下簡單的實體:FindByUUID()使用Spring Data的JPA庫
@Entity
@Table(name = "simple_entity")
@Access(AccessType.FIELD)
public class SimpleEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
@Column(unique = true, updatable = false)
protected UUID uuid;
@PrePersist
protected void onCreateAbstractBaseEntity() {
this.uuid = UUID.randomUUID();
}
public Long getId() {
return this.id;
}
public UUID getUuid() {
return this.uuid;
}
}
Spring數據JPA與Hibernate創建一切正確在我的MySQL數據庫。但是,當我嘗試使用我的JPARepository實現來使用它的uuid搜索一個項目時,即使它在數據庫上執行find查詢(我可以在調試器中看到它),它也找不到任何東西。這裏是我的JPARepository實現:
public interface SimpleEntityRepository extends JpaRepository<SimpleEntity, Long> {
SimpleEntity findOneByUuid(UUID uuid);
}
這是調用此方法的控制器。
@Controller
@RequestMapping("/simple_entity")
public class SimpleEntityController {
@Autowired
private SimpleEntityRepository repository;
@RequestMapping(method = RequestMethod.GET, value = "/{simpleEntityId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<FileDatastore> getSimpleEntity(@PathVariable UUID simpleEntityId) {
SimpleEntity record = this.repository.findOneByUuid(simpleEntityId);
HttpHeaders headers = new HttpHeaders();
HttpStatus status = (record != null) ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<>(record, headers, status);
}
我錯過了什麼嗎?
感謝您的幫助!
向我們展示您在調用方法時的實現和相關代碼。 – DannyMo