0

Please refer attached screenshot to understand the table structure.

enter image description here

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

Vinod Kumar
  • 535
  • 1
  • 7
  • 22

2 Answers2

1

The two entities Product and Employee don't have any connection as far as JPA is concerned. Therefore you can't access both through a single repository.

If for example, Product would have an actual reference to an Employee you could use a ProductRepository to load Products and navigate from there to the referenced Employees.

But even if that might be feasible, I'd guess that Product and Employee should be considered different aggregates and therefore, should have their own repository each. See Are you supposed to have one repository per table in JPA? for more information on that question.

Given the entities, your repositories look just fine. Note that the entities do look atypical due to the use of String productId instead of Product product.

Jens Schauder
  • 65,795
  • 24
  • 148
  • 294
0

If you wanted to fetch the employee details, you need the following interface,

public interface MyMainDataRepository extends JpaRepository<Employee, String> {
}

If you wanted to fetch the product details, you need the following interface,

public interface MyRepository extends JpaRepository<Product, EmpProd> {
}

The employee is related to product table, the iteration happens via product and related employees. From this, you can not access the employee table directly and retrieve the employee results from MyRepository interface.

Gowthami Reddy
  • 410
  • 4
  • 20