for some reason I have not being able to find a suitable answer for this. I have the following simple entity:
@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 Data JPA with Hibernate creates everything correctly in my MySQL database. However, when I try to use my JPARepository implementation to search for an item using its uuid, it never finds anything, even though it executes the find query on the DB (which I can see in my debugger). Here is my JPARepository implementation:
public interface SimpleEntityRepository extends JpaRepository<SimpleEntity, Long> {
SimpleEntity findOneByUuid(UUID uuid);
}
Here is the controller that calls this method.
@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);
}
Am I missing something?
Thanks for your help!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…