gpt4 book ai didi

java - 使用 JOIN 实现 JPA 请求

转载 作者:行者123 更新时间:2023-12-02 10:09:02 26 4
gpt4 key购买 nike

我有这 3 个实体:

付款交易:

@Entity
@Table(name = "payment_transactions")
public class PaymentTransactions implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;
.....
}

WPF 付款:

@Entity
@Table(name = "wpf_payments")
public class WpfPayments implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;
............
}

WPF Payments 付款交易:

@Entity
@Table(name = "wpf_payment_payment_transactions")
public class WpfPaymentPaymentTransactions implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, nullable = false, length = 3)
private int id;

@Column(length = 4)
private Integer wpf_payment_id;

@Column(length = 4)
private Integer payment_transaction_id;
.....
}

我使用这些 SQL 请求来获取基于 id 的正确数据:

SELECT  `payment_transactions`.* FROM `payment_transactions` INNER JOIN `wpf_payment_payment_transactions` ON `payment_transactions`.`id` = `wpf_payment_payment_transactions`.`payment_transaction_id` WHERE `wpf_payment_payment_transactions`.`wpf_payment_id` = 75  ORDER BY `payment_transactions`.`id` ASC LIMIT 1


SELECT `payment_transactions`.* FROM `payment_transactions` INNER JOIN `wpf_payment_payment_transactions` ON `payment_transactions`.`id` = `wpf_payment_payment_transactions`.`payment_transaction_id` WHERE `wpf_payment_payment_transactions`.`wpf_payment_id` = 75

是否有某种方法可以使用 JPA 查询来实现这些 SQL 请求?

最佳答案

如果您使用的是 JPA 2.0,则无法在查询中使用 JPQL,因为您无法使用 ON 子句。

一种解决方案是在实体 WpfPaymentPaymentTransactions 上实现双向映射, PaymentTransactions 能够进行加入:

 @Entity
@Table(name = "payment_transactions")
public class PaymentTransactions implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, nullable = false)
private int id;

@OneToOne(mappedBy="paymentTransactions") //or OneToMany depending on your model
private WpfPaymentPaymentTransactions wpfPaymentPaymentTransactions;

}


@Entity
@Table(name = "wpf_payment_payment_transactions")
public class WpfPaymentPaymentTransactions implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, nullable = false, length = 3)
private int id;

@ManyToOne
@JoinColumn(name="wpf_payment_id")
private PaymentTransactions paymentTransactions;
}

然后您可以像这样连接两个实体:

  SELECT t FROM PaymentTransactions t 
INNER JOIN WpfPaymentPaymentTransactions wppt

从 JPA 2.1 开始,您可以在 JPQL 查询中添加 ON 子句。因此,对于第一个查询,它将是这样的:

      SELECT t FROM PaymentTransactions t 
INNER JOIN WpfPaymentPaymentTransactions wppt
ON t.id = wppt.paymentTransactionId`
WHERE wppt.wpfPaymentId = :param
ORDER BY t.id ASC LIMIT 1

希望对你有帮助!

关于java - 使用 JOIN 实现 JPA 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55125816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com