Please refer attached screenshot to understand the table structure.
Empd_Id is the primary key in 'Employee' table which in turn becomes as a part of composite key along with 'product_id' in table called 'product'. Any employee can have multiple products so in that case it becomes 'One-to-Many' relationship between 'Employee-Product' tables. Now I'm confused whether I need to write just 1 JpaRepository interface i.e. for employee or 2 JpaRepository interfaces (1 for Employee and another for Product). My gut feeling is just 1 interface for Employee table but how???
Following is my code snippet:- 1st JPA repository interface
public interface MyRepository extends JpaRepository<Product, EmpProd> {
}
Entity:-
@Entity
@Table(name="product")
public class Product{
@EmbeddedId
private EmpProd empProd;
@Column(name="product_name")
private String commerceUserId;
@Column(name="description")
private String description;
For composite keys:-
@Embeddable
public class EmpProd implements Serializable{
private static final long serialVersionUID = 1L;
@NotNull
@Column(name="emp_id")
private String empId;
@NotNull
@Column(name="product_id")
private String productId;
2nd Jpa repository interface
public interface MyMainDataRepository extends JpaRepository<Employee, String> {
}
Entity class:-
@Entity
@Table(name="employee")
public class Employee{
@Id
@NotNull
@Column(name="emp_id")
private String empId;
@Column(name="first_name")
private String firstName;
Though, I have written 2 separate JPA repositories, I strongly believe there will be need for just 1, the main one i.e.
public interface MyMainDataRepository extends JpaRepository { }
But I do not know to related both entity classes and fetch data from using single Jpa repository as I'm new to Spring Data JPA. I would really appreciate if someone can help me here. Thanks