简体   繁体   中英

Spring Boot JPA, data do not persist in join table of many-to-many relationship

I need some help/advice since I am new to Spring Boot. I am trying to make many-to-many relationship with the help of linking table. But for some reason, I can not persist data in the link table.

Here are the entities:

@Table(name = "providers")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor

public class ProviderEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long providerId;

    @OneToMany(mappedBy = "provider", fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<ProviderPractitionersEntity> providerPractitioners; 

    ...
-------------------------------------------------------------
@Entity
@Table(name = "company_practitioner_types")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor

public class CompanyPractitionerTypeEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "practitioner", fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<ProviderPractitionersEntity> practitionerProviders;

    ...
}
---------------------------------------------------------------
@Entity
@Table(name = "provider_practitioners")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ProviderPractitionersEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JsonBackReference
    @JoinColumn(name = "practitioner_id") /*this column is foreign key from practitioner table*/
    private CompanyPractitionerEntity practitioner;

    @ManyToOne
    @JsonBackReference
    @JoinColumn(name = "provider_id") /*this column is foreign key from provider table*/
    private ProviderEntity provider;

    @Column(name = "size")
    private String size;

}

When I am persisting new Provider, I set this new provider object's reference and practitioner object's reference in every ProviderPractitioner object before persisting.

As the result, objects from List providerPractitioners have null values for all states. And nothing is persisted to provider_practitioners table in the database.

The reason I am trying to set many-to-many relationship this way, instead of using @ManyToMany annotation is because of the "size" variable in ProviderPractitionerEntity which contains number of one type of practitioners for one provider.

I have tried to create embededId (composite ID) for linking table, and got the same result.

You have to create the @EmbeddedId as you mentioned, and then in the ProviderPractitionersEntity subentities (the both referenced) add @MapsId with the name of the property in the composite id

First create the composite id:

@Embeddable
public class ProviderPractitionersId implements Serializable {

   @Column(name = "practitioner_id")
   private Long practitionerId;

   @Column(name = "provider_id")
   private Long providerId;

And then, in the many-to-many entity map the id and both entities this way:

    @EmbeddedId
    private ProviderPractitionersId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("practitionerId")
    private CompanyPractitionerTypeEntity practitioner;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("providerId")
    private ProviderEntity provider;

And so, in the ProviderPractitionersEntity you can add, as you mentioned, as many properties as you like, like your size or anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM