使用 JOIN 实现 JPA 请求
Implement JPA request with JOIN
我有这 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 付款付款交易:
@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
希望对您有所帮助!
我有这 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 付款付款交易:
@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
希望对您有所帮助!