出于某些原因,我一直没有找到一个合适的答案。我有以下简单实体:
@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;
}
}
使用Hibernate的Spring Data JPA在我的MySQL数据库中创建了一切。然而,当我尝试使用我的JPARepository实现通过uuid搜索项目时,它从来没有找到任何东西,即使它在数据库中执行了查找查询(我可以在调试器中看到)。以下是我的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);
}
我有点不明白,还有什么需要注意的吗?
感谢您的帮助!