0

I'm writing 3 tables in the following relation:

enter image description here

Club class:

@Setter
@Getter
@Entity
@Table(name = "Club")
public class Club {
    @Id
    @GeneratedValue
    private Long id;

    private String name;
    private String type;
    private String mainPage;
    private String logo;

    @OneToMany(mappedBy="clubProductKey.club", cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = "clubProductKey.club", allowSetters=true)
    private Set<ClubProduct> clubProducts;
...

Product class:

@Setter
@Getter
@Entity
@Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy="clubProductKey.product", cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = "clubProductKey.product", allowSetters=true)
    private Set<ClubProduct> clubProducts;
...

ClubProduct class:

@Setter
@Getter
@Entity
@Table(name = "ClubProduct")
public class ClubProduct {
    @EmbeddedId
    private ClubProductKey clubProductKey;
...

ClubProductKey class:

@Setter
@Getter
@Embeddable
public class ClubProductKey implements Serializable {
    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH })
    @JoinColumn(name = "club_id", referencedColumnName = "id")
    @JsonIgnoreProperties(value = "clubProducts", allowSetters=true)
    private Club club;

    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH })
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @JsonIgnoreProperties(value = "clubProducts", allowSetters=true)
    private Product product;
...

ClubProductRepository class:

public interface ClubProductRepository extends JpaRepository<ClubProduct, ClubProductKey> {
    public List<ClubProduct> findByClubProductKeyClub(Club club);
    public List<ClubProduct> findByClubProductKeyProduct(Product product);
}

I try to save clubProduct like this:

@Service
public class ClubProductServiceImp implements ClubProductService {
    @Autowired
    private ClubProductRepository clubProductRepository;
    ...
    ClubProduct savedClubProduct = clubProductRepository.save(clubProduct);
    return savedClubProduct;
}

However I find that the clubProduct is not saved in the clubProducts list in the club or product entity, the list is null. Must I add lines like club.getClubProducts.add(clubProduct) or is there any other way to make it added automatically?

Thank you.

1 Answers1

3

The @OnetoMany mapping in your Club class uses the attribute mappedby which means that it represents the owning side of the relation responsible for handling the mapping. However, we still need to have both sides in sync as otherwise, we break the Domain Model relationship consistency, and the entity state transitions are not guaranteed to work unless both sides are properly synchronized.

The answer is yes, you have to manage the java relations yourself so that the clubProducts gets persisted. You are using an instance of the repository class club to persist the data so , you should add a setter method like :

  public void addClubProduct(ClubProduct clubProduct) {
     if (clubProduct!= null) {
        if (clubProduct== null) {
            clubProduct= new ArrayList<ClubProduct>();          
        }
        clubProducts.add(clubProduct);
        clubProduct.setClubProduct(this);
     }
  }

also a method to remove it from the list and use these method in your code to set the values to the list properly before initiating save . Read related article

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39