在我的项目中,对象的一个字段是UUID ( Java.util.uuid ),当我尝试使用find by(Myfield)时,它总是返回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);}
用于检查存储库的控制器:公共类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() )
这没有任何意义。
有人能帮我吗?我猜问题一定出在hibernate本身,也许他不知道从MYSQL数据库中读取UUID。
发布于 2021-07-18 17:17:07
您可以将日志添加到应用程序中
# 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可以使用哪种类型?
https://stackoverflow.com/questions/68427441
复制相似问题