如何在Spring Boot JPA中使用Flyway实现一对多关系。

huangapple go评论59阅读模式
英文:

How to implement One to Many relationship in Spring Boot JPA using Flyway

问题

我想要为下面的实体建立一对多的关系。(membership_plan可以有多个membership_plan_features)。我正在使用Spring Boot 3.0.6。

@Entity
@Table(name = "membership_plan")
public class MembershipPlan {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long planId;

    @Column(name = "org_id")
    private String orgId;

    @Column(name = "plan_name")
    private String planName;

    @Column(name = "plan_price")
    private String planPrice;

    @OneToMany(mappedBy = "membershipPlan")
    private List<MembershipPlanFeature> planFeature;
}

第二个实体

@Entity
@Table(name = "membership_plan_feature")
@Getter
@Setter
public class MembershipPlanFeature {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "org_id")
    private String orgId;

    @Column(name = "feature_name")
    private String featureName;

    @Column(name = "feature_desc")
    private String featureDesc;

    @ManyToOne
    @JoinColumn(name="plan_id")
    private MembershipPlan membershipPlan;
}

以下是Flyway查询:

CREATE TABLE membership_plan (
   plan_id INT NOT NULL,
   org_id VARCHAR(255) NULL,
   plan_name VARCHAR(255) NULL,
   plan_price VARCHAR(255) NULL,
   PRIMARY KEY (plan_id)
);

CREATE TABLE membership_plan_feature (
   id INT AUTO_INCREMENT NOT NULL,
   org_id VARCHAR(255) NULL,
   plan_id INT NOT NULL,
   feature_name VARCHAR(255) NULL,
   feature_desc VARCHAR(255) NULL,
   PRIMARY KEY (id)
);

ALTER TABLE membership_plan_feature
ADD CONSTRAINT fk_membership_plan_feature
FOREIGN KEY (plan_id)
REFERENCES membership_plan (plan_id);

现在,当我查询 membershipPlanRepository.findAll(); 时,返回了递归数据:

[
    {
        "planId": 1,
        "orgId": "101",
        "planName": "GOLD",
        "planPrice": "29",
        "planFeature": [
            {
                "id": 1,
                "orgId": "101",
                "featureName": "Kanban",
                "featureDesc": "Kanban",
                "membershipPlan": {
                    "planId": 1,
                    "orgId": "101",
                    "planName": "GOLD",
                    "planPrice": "29",
                    "planFeature": [
                        {
                            "id": 1,
                            "orgId": "101",
                            "featureName": "Kanban",
                            "featureDesc": "Kanban",
                            "membershipPlan": {
                                "planId": 1,
                                "orgId": "101",
                                "planName": "GOLD",
                                "planPrice": "29",
                                "planFeature": [... 和后续数据

以下是异常信息:

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)]

请告诉我我做错了什么地方。

英文:

I want to have one-to-many relationship for the below entities.(membership_plan can have multiple membership_plan_features). I am using spring boot 3.0.6.

@Entity
@Table(name = &quot;membership_plan&quot;)
public class MembershipPlan {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long planId;

    @Column(name = &quot;org_id&quot;)
    private String orgId;

    @Column(name = &quot;plan_name&quot;)
    private String planName;

    @Column(name = &quot;plan_price&quot;)
    private String planPrice;

    @OneToMany (mappedBy = &quot;membershipPlan&quot;)
    private List&lt;MembershipPlanFeature&gt; planFeature;
}

Second entity

@Entity
@Table(name = &quot;membership_plan_feature&quot;)
@Getter
@Setter
public class MembershipPlanFeature {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = &quot;org_id&quot;)
    private String orgId;

    @Column(name = &quot;feature_name&quot;)
    private String featureName;

    @Column(name = &quot;feature_desc&quot;)
    private String featureDesc;

    @ManyToOne
    @JoinColumn(name=&quot;plan_id&quot;)
    private MembershipPlan membershipPlan;
}

Below are the flyway queries:

CREATE TABLE membership_plan (
   plan_id INT NOT NULL,
   org_id VARCHAR(255) NULL,
   plan_name VARCHAR(255) NULL,
   plan_price VARCHAR(255) NULL,
   PRIMARY KEY (plan_id)
   );

CREATE TABLE membership_plan_feature (
   id INT AUTO_INCREMENT NOT NULL,
   org_id VARCHAR(255) NULL,
   plan_id INT NOT NULL,
   feature_name VARCHAR(255) NULL,
   feature_desc VARCHAR(255) NULL,
   PRIMARY KEY (id)
);

ALTER table membership_plan_feature
ADD CONSTRAINT fk_membership_plan_feature
FOREIGN KEY (plan_id)
REFERENCES membership_plan (plan_id);

Now, when I query membershipPlanRepository.findAll(); it return me a recursive data:

[
    {
        &quot;planId&quot;: 1,
        &quot;orgId&quot;: &quot;101&quot;,
        &quot;planName&quot;: &quot;GOLD&quot;,
        &quot;planPrice&quot;: &quot;29&quot;,
        &quot;planFeature&quot;: [
            {
                &quot;id&quot;: 1,
                &quot;orgId&quot;: &quot;101&quot;,
                &quot;featureName&quot;: &quot;Kanban&quot;,
                &quot;featureDesc&quot;: &quot;Kanban&quot;,
                &quot;membershipPlan&quot;: {
                    &quot;planId&quot;: 1,
                    &quot;orgId&quot;: &quot;101&quot;,
                    &quot;planName&quot;: &quot;GOLD&quot;,
                    &quot;planPrice&quot;: &quot;29&quot;,
                    &quot;planFeature&quot;: [
                        {
                            &quot;id&quot;: 1,
                            &quot;orgId&quot;: &quot;101&quot;,
                            &quot;featureName&quot;: &quot;Kanban&quot;,
                            &quot;featureDesc&quot;: &quot;Kanban&quot;,
                            &quot;membershipPlan&quot;: {
                                &quot;planId&quot;: 1,
                                &quot;orgId&quot;: &quot;101&quot;,
                                &quot;planName&quot;: &quot;GOLD&quot;,
                                &quot;planPrice&quot;: &quot;29&quot;,
                                &quot;planFeature&quot;: [... and so on

below is the exception:

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError)]

Please let me know where I am doing it wrong.

答案1

得分: 1

你必须告诉Jackson跳过为你的实体生成嵌套字段:

...
@ManyToOne
@JoinColumn(name = "plan_id")
@JsonBackReference // 添加这个
private MembershipPlan membershipPlan;
...
...
@OneToMany(mappedBy = "membershipPlan")
@JsonManagedReference // 添加这个
private List<MembershipPlanFeature> planFeature;
...
英文:

You have to tell Jackson to skip generate nested fields to your entities:

    ...
    @ManyToOne
    @JoinColumn(name = &quot;plan_id&quot;)
    @JsonBackReference // Add this
    private MembershipPlan membershipPlan;
    ...
    ...
    @OneToMany(mappedBy = &quot;membershipPlan&quot;)
    @JsonManagedReference // Add this
    private List&lt;MembershipPlanFeature&gt; planFeature;
    ...

huangapple
  • 本文由 发表于 2023年6月5日 18:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定