【问题标题】:JPA method for mapped column映射列的 JPA 方法
【发布时间】: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


    【解决方案1】:

    应该是这样的。 IDE 自动建议会在您键入时提供帮助,以防万一。

    findFinancialSubPlanByFinancialPlanDaoId(int internalId)
    

    findFinancialSubPlanByFinancialPlanDao(int internalId)
    

    【讨论】:

    • 你的回答引导我形成正确的方法:::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::: : List findFinancialSubPlanByFinancialPlanInternalId(int id);
    • 很高兴这个答案对你有所帮助!
    猜你喜欢
    • 2021-03-20
    • 2013-04-14
    • 2012-01-04
    • 2011-07-13
    • 2019-03-27
    • 1970-01-01
    • 2023-03-27
    • 2019-10-28
    • 1970-01-01
    相关资源
    最近更新 更多