Home > Enterprise >  One to many - many to one relationship in Spring JPA
One to many - many to one relationship in Spring JPA

Time:01-17

I have this Coupon Class

package Kinn.College.CouponManagementSystem.entities;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table (name = "companies")
public class Company {
    @Id
    @GeneratedValue (strategy =  GenerationType.IDENTITY)
    @Column (name = "company_id")
    private int id; // id of the company
    @Column (name = "company_name")
    private String name; // name of the company
    @Column (name = "company_email")
    private String email; // email of the company
    @Column (name = "company_password")
    private String password; // password of the company

    @OneToMany(mappedBy = "company_id", cascade = CascadeType.ALL)
    private List<Coupon> coupons;


}

and I have this company class

package Kinn.College.CouponManagementSystem.entities;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table (name = "companies")
public class Company {
    @Id
    @GeneratedValue (strategy =  GenerationType.IDENTITY)
    @Column (name = "company_id")
    private int id; // id of the company
    @Column (name = "company_name")
    private String name; // name of the company
    @Column (name = "company_email")
    private String email; // email of the company
    @Column (name = "company_password")
    private String password; // password of the company

    @OneToMany(mappedBy = "company_id", cascade = CascadeType.ALL)
    private List<Coupon> coupons;


}

for some reason, the one to many - many to one relationship creates an error when I try to get a company from the DB with a list of coupons.

I'm using this syntax to get a copmany from DB.

{
Company company = companyService.getCompanyById(1);
        System.out.println("Got company by id: "   company);
}

if I'm removing the list of coupons from every company, it works just fine. This is the error message;

2023-01-16T11:59:33.266 02:00 ERROR 16808 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Collection 'Kinn.College.CouponManagementSystem.entities.Company.coupons' is 'mappedBy' a property named 'company_id' which does not exist in the target entity 'Kinn.College.CouponManagementSystem.entities.Coupon'


I've tried asking multiple people know Spring very well and all of them say it looks okay, that it should work.

CodePudding user response:

mapped_by reference the property or the referenced class, not the column name:

@OneToMany(mappedBy = "id", cascade = CascadeType.ALL)
private List<Coupon> coupons;

BTW: Take care of java naming conventions. Package names should only contains lower case character

CodePudding user response:

I am assuming that a company can relate to many coupons.

So in Coupon class, you can have a Company member which represents the company it belongs to :

@ManyToOne
@JoinColumn(name = "company_id")
private Company company;

Here, company_id would become the foreign key that points to primary key of company table

And in Company class, you can have List of coupons that it owns:

@OneToMany(mappedBy = "company", orphanRemoval = true)
private List<Coupon> coupons = new ArrayList<>();

CodePudding user response:

The Company entity should have a field to point to all its coupons as shown below:

@OneToMany(mappedBy="company")
private Set<Coupon> coupons;

The mappedBy field is the instance variable name we'll use in Coupon entity to point to the associated Company.

The Coupon entity should have a field to point to all its coupons like:

@ManyToOne
@JoinColumn(name="company_id", nullable=false)
private Company company;

The name field refers to the name of the foreign key in the Coupon table that points to the Company table.

You can check this article https://www.baeldung.com/hibernate-one-to-many.

  • Related