简体   繁体   English

Spring 启动 JPA,数据不保存在多对多关系的连接表中

[英]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.我需要一些帮助/建议,因为我是 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.当我持久化新提供者时,我在持久化之前在每个 ProviderPractitioner object 中设置了这个新提供者对象的引用和从业者对象的引用。

As the result, objects from List providerPractitioners have null values for all states.因此,来自 List providerPractitioners 的对象具有所有状态的 null 值。 And nothing is persisted to provider_practitioners table in the database.并且没有任何东西被持久化到数据库中的 provider_practitioners 表中。

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.我试图以这种方式设置多对多关系而不是使用 @ManyToMany 注释的原因是因为 ProviderPractitionerEntity 中的“大小”变量包含一个提供者的一种类型从业者的数量。

I have tried to create embededId (composite ID) for linking table, and got the same result.我试图为链接表创建 embededId(复合 ID),并得到了相同的结果。

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您必须按照您提到的那样创建@EmbeddedId ,然后在ProviderPractitionersEntity子实体(两者都被引用)中添加@MapsId以及复合 id 中的属性名称

First create the composite id:首先创建复合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:然后,在多对多实体 map 中,id 和两个实体都是这样:

    @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.因此,正如您所提到的,您可以在ProviderPractitionersEntity中添加任意数量的属性,例如您的size或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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