首页 > 解决方案 > 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。

标签: javamysqlhibernatehibernate-mapping

解决方案


您可以将日志添加到您的应用程序

# 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使用什么样的库尔姆类型?


推荐阅读