Masoom Raza
Masoom Raza

Reputation: 179

JPA method for mapped column

I am trying to get how to write the JPA method for the class by using its foreign key instead of the primary key. Like, here I can't use findById() method, as it finds records according to primary key defined in the class. Below are the two classes for @ManyToOne and @OneToMany.

PARENT CLASS :

@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;
}

CHILD CLASS:

@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; 
}

The table generated for FinancialSubPlan will consist of the primary key column "subplan_id" and foreign key column "Internal_plan_id". So is there any way to write the JPA method to get records of FinancialSubPlan by "internal_plan_id". Also how to get this using @Query ?

Upvotes: 0

Views: 110

Answers (1)

Anil Kumar Athuluri
Anil Kumar Athuluri

Reputation: 653

It would be something like this. IDE auto-suggest would help as you type, just in case.

findFinancialSubPlanByFinancialPlanDaoId(int internalId)

or

findFinancialSubPlanByFinancialPlanDao(int internalId)

Upvotes: 1

Related Questions