【发布时间】:2021-10-12 01:33:25
【问题描述】:
我正在尝试通过使用其外键而不是主键来了解如何为该类编写 JPA 方法。就像,在这里我不能使用 findById() 方法,因为它根据类中定义的主键查找记录。下面是@ManyToOne 和@OneToMany 的两个类。
父类:
@Entity
@Getter
@Setter
//@Data
@NoArgsConstructor
@Table(name = "financial_plan_details", schema = "financialplanadmin")
public class FinancialPlanDao {
// This internalId is the primary key of the table.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "internal_plan_id")
private int internalId;
// This stores the plan status into the database table.
@Column(name = "plan_status")
@Size(max = 10)
private String planStatus;
@Column(name = "presentation_file_key")
@Size(max = 500)
private String presentationFileKey;
@Column(name = "create_timestamp")
@NotNull
private Timestamp createdTimestamp;
@OneToMany(mappedBy = "financialPlan")
private List<FinancialSubPlan> subPlans;
}
儿童班:
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "financial_plan_subplan", schema = "financialplanadmin")
@JsonInclude(Include.NON_NULL)
public class FinancialSubPlan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "subplan_id")
private int subPlanId;
@Column(name = "external_subplan_id")
private String externalSubplanId;
@Column(name = "is_chosen")
private Boolean subPlanIsChosen;
@ManyToOne
@JoinColumn(name = "internal_plan_id")
private FinancialPlanDao financialPlan;
}
为 FinancialSubPlan 生成的表将由主键列“subplan_id”和外键列“Internal_plan_id”组成。那么有没有办法编写JPA方法来通过“internal_plan_id”获取FinancialSubPlan的记录。还有如何使用@Query 来获得这个?
【问题讨论】:
标签: java mysql spring-boot hibernate jpa