java - findBy UUID 字段始终返回 null 。(MySQL + JAVA + 休眠)
问题描述
在我的项目中,当我尝试使用 find by(Myfield) 时,对象的字段之一是 UUID ( Java.util.uuid ),即使对象存在,它也始终返回 null。
我添加了我的代码和示例来明确问题:
我的实体:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "RH_item_id")
private int item_id;
@Column(name = "RH_order_id")
private int orderId;
@Column(name = "RH_partner_order_id",columnDefinition = "BINARY(16)")
@Type(type = "uuid-binary")
private UUID partnerOrderId;
我的存储库:
public interface TransactionRepository extends JpaRepository<Transaction, Long> {
List<Transaction> findByDate(Date date);
List<Transaction> findByAgent(Agent agent);
List<Transaction> findByFreelancer(Freelancer freelancer);
List<Transaction> findByStatus(Status status);
Transaction findByPartnerOrderId(UUID id);
Transaction findByOrderId(int id);
List<Transaction> findByEmail(String email);
}
我的控制器检查存储库: public class AgentController {
@Autowired
private AgentService agentService;
@Autowired
private TransactionRepository transactionRepository;
@PreAuthorize("hasRole('ROLE_AGENT')")
@RequestMapping(value = "/getAllTrans", method = RequestMethod.GET)
public ResponseEntity<?> getAllTransactions() {
try {
List<Transaction> Lot = agentService.getAllTransactions();
System.out.println(transactionRepository.findByPartnerOrderId(Lot.get(4).getPartnerOrderId()));
return ResponseEntity.ok(Lot);
} catch (TransactionsNotFoundException e) {
e.printStackTrace();
}
return ResponseEntity.badRequest().body("There are no transactions found.");
}
如果您注意到即使我从对象本身(Lot)获取字段,它也会返回 null。( transactionRepository.findByPartnerOrderId(Lot.get(4).getPartnerOrderId() )
这没有任何意义...
任何人都可以帮助我吗?我想问题一定来自休眠本身,也许他不知道从 MYSQL DB 中读取 UUID。
解决方案
您可以将日志添加到您的应用程序
# logs the SQL statements
log4j.logger.org.hibernate.SQL=debug
# Logs the JDBC parameters passed to a query
log4j.logger.org.hibernate.type=trace
并使用它使用的参数观察 sql。
db中的uuid使用什么样的库尔姆类型?
推荐阅读
- xcode - Xcode 10 中新的 Apple Watch“主屏幕”和“短视”图像资产尺寸是多少?
- angular - 如何在cloud9上的角度项目中设置自动重新加载?
- codeigniter - 访问 view/admin/sub_folder 中的图像
- c# - 如何在 C# 中创建选择加入方法继承?
- node.js - 已经在对话框中时如何处理“取消”意图?
- python - 使用python在docx文档中删除带有“删除线”字体的文本
- jquery - Laravel-5.2 Css 和 js 在没有资产的情况下无法工作
- plsql - 我想搜索名字,它会显示全名
- css - 无法让 webpack 输出捆绑的 css?
- python-3.x - 仅使用 tf.data API(无 feed_dict)的高性能输入管道的最佳实践