Man*_*Bey 3 mysql spring hibernate jpa 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;
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Data JPA with Hibernate 在我的 MySQL 数据库中正确创建了所有内容。但是,当我尝试使用我的 JPARepository 实现来搜索使用其 uuid 的项目时,它永远找不到任何东西,即使它在数据库上执行查找查询(我可以在调试器中看到)。这是我的 JPARepository 实现:
public interface SimpleEntityRepository extends JpaRepository<SimpleEntity, Long> {
SimpleEntity findOneByUuid(UUID uuid);
}
Run Code Online (Sandbox Code Playgroud)
这是调用此方法的控制器。
@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);
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
谢谢你的帮助!
尝试UUID使用@org.hibernate.annotations.Type(type="org.hibernate.type.UUIDCharType")
或注释您的财产@org.hibernate.annotations.Type(type="org.hibernate.type.UUIDBinaryType")
UUID由于 MSB/LSB 以UUID二进制格式交换,我在查询数据库时遇到了类似的问题;我们解决了将数据视为字符串的问题,并在转换前进行必要的转换。
将二进制列更改为字符串。默认为二进制,您必须添加此附加符号
@Type(type="org.hibernate.type.UUIDCharType")
Run Code Online (Sandbox Code Playgroud)
例如。
@Id
@GeneratedValue
@Type(type="org.hibernate.type.UUIDCharType")
public UUID id;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11294 次 |
| 最近记录: |