java Sping Boot 双向OneToMany关系未按预期序列化/反序列化

hc2pp10m  于 2023-05-15  发布在  Java
关注(0)|答案(1)|浏览(127)

所以我在Sping Boot 中遇到了关系问题,我不知道该怎么办了。
我有这样的问题,比如,参与者和公司之间的双向关系解决得很好,但参与者和事件之间的双向关系根本没有解决好。
此外,在参与者和公司之间,一切都很好地Map,并在双方进行序列化/反序列化。
但是在参与和事件之间,事件只为参与保存,而参与不为事件保存。
这让我很惊讶,因为我实际上对这两种情况都有相同的注解。
这是我的参与课:

@Entity
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope = Participation.class)
@Table(name = "Participations")
public class Participation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @NonNull@Column(nullable = false)
    private double price;
    @NonNull@Column(nullable = false)
    private double paidUntilNow;
    @NonNull@Column(nullable = false)
    private String comments;
    @NonNull@Column(nullable = false, name = "`if`")
    private boolean IF;
    @NonNull@Column(nullable = false)
    private boolean IT;
    @NonNull@Column(nullable = false)
    private boolean BT;
    @NonNull@Column(nullable = false)
    private boolean IR;
    @NonNull@Column(nullable = false)
    private int cntTables;
    @NonNull@Column(nullable = false)
    private boolean needsPower;
    @NonNull@Column(nullable = false)
    private boolean ownBooth;
    @NonNull@Column(nullable = false)
    private int needsSpace;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Company company;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Event event;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Booth booth;
}

这是我的Event类:

@Entity
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope = Event.class)
@Table(name = "Events")
public class Event {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @NonNull@Column(name="`when`",nullable = false)
    private LocalDate when;
    @NonNull@Column(unique = true, nullable = false)
    private String label;
    @NonNull@Column(nullable = false)
    private double boothPrice;
    @NonNull@Column(nullable = false)
    private double bigBoothPrice;
    @OneToMany(mappedBy = "event", fetch = FetchType.LAZY)
    private Set<Participation> participations;
}

这是我的公司类:

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
@SuperBuilder
@JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id", scope = Company.class)
@Table(name = "Companies")
public class Company extends User{
    @Column
    private String responsible;
    @Column
    private String phone;
    @Column
    private String ccTo; //Email
    @Column
    private String comments = "";
    @Column
    private boolean isPartner;
    @Column
    private boolean isAdmin = false;
    @OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
    private Set<Participation> participations;
    @OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
    private Set<Mail> receivedMails;
}

以下是我处理参与发布请求的方式:

@PostMapping("/")
    public ResponseEntity<Object> create(@RequestBody Participation p) {
        ResponseEntity<Object> resp = null;
        p.setBooth(boothService.getBooth(p.getBooth().getId()));
        p.setCompany(companyService.getCompany(p.getCompany().getId()));
        p.setEvent(eventService.getEvent(p.getEvent().getId()));
        if(p.getEvent() == null) {
            return new ResponseEntity<Object>("Event not found", HttpStatus.BAD_REQUEST);
        }
        if(p.getCompany() == null) {
            return new ResponseEntity<Object>("Company not found", HttpStatus.BAD_REQUEST);
        }
        if(p.getBooth() == null) {
            return new ResponseEntity<Object>("Booth not found", HttpStatus.BAD_REQUEST);
        }
        resp = new ResponseEntity<Object>(participationService.createParticipation(p), HttpStatus.CREATED);
        return resp;
    }

现在,当我对一个事件发出GET请求时,我会得到这样的响应:

{
        "id": 1,
        "when": "2023-12-10",
        "label": "Wirtschaftstag 2023",
        "boothPrice": 90.0,
        "bigBoothPrice": 180.0,
        "participations": []
    }

当我对一个Participation发出GET请求时,我得到的响应是:

{
        "id": 1,
        "price": 100.0,
        "paidUntilNow": 50.0,
        "comments": "Wir benötigen zwei Tische und einen Stromanschluss.",
        "cntTables": 2,
        "needsPower": true,
        "ownBooth": false,
        "needsSpace": 6,
        "company": {
            "id": 3,
            "name": "Name",
            "email": "office@name.com",
            "pwdToken": "Name123",
            "responsible": "Harcel Muger",
            "phone": "+43 699 32313456",
            "ccTo": "hugo@gmail.com",
            "comments": "",
            "participations": [
                1
            ],
            "receivedMails": [],
            "admin": false,
            "partner": false
        },
        "event": {
            "id": 1,
            "when": "2023-12-10",
            "label": "Wirtschaftstag 2023",
            "boothPrice": 90.0,
            "bigBoothPrice": 180.0,
            "participations": []
        },
        "booth": {
            "id": 1,
            "nr": "1",
            "size": 8,
            "big": false,
            "top": 0,
            "left": 0,
            "width": 2,
            "height": 4,
            "participations": [
                1
            ]
        },
        "bt": false,
        "if": true,
        "it": false,
        "ir": false
    }

举个例子,公司:

{
            "id": 3,
            "name": "Name",
            "email": "office@name.com",
            "pwdToken": "Name123",
            "responsible": "Harcel Muger",
            "phone": "+43 699 32313456",
            "ccTo": "hugo@gmail.com",
            "comments": "",
            "participations": [
            {
                "id": 1,
                "price": 100.0,
                "paidUntilNow": 50.0,
                "comments": "Wir benötigen zwei Tische und einen Stromanschluss.",
                "cntTables": 2,
                "needsPower": true,
                "ownBooth": false,
                "needsSpace": 6,
                "company": 3,
                "event": {
                    "id": 1,
                    "when": "2023-12-10",
                    "label": "Wirtschaftstag 2023",
                    "boothPrice": 90.0,
                    "bigBoothPrice": 180.0,
                    "participations": []
                },
                "booth": {
                    "id": 1,
                    "nr": "1",
                    "size": 8,
                    "big": false,
                    "top": 0,
                    "left": 0,
                    "width": 2,
                    "height": 4,
                    "participations": [
                        1
                    ]
                },
                "bt": false,
                "if": true,
                "it": false,
                "ir": false
            }
        ],
        "receivedMails": [],
        "admin": false,
        "partner": false
    }
        }

在事件中,您可以看到它并没有保存参与。尽管在公司中,它如预期的那样节省了参与。这让我抓狂,因为我对两个关系使用了相同的注解。也许有人能帮我

vecaoik1

vecaoik11#

在Event中,将participations定义为FetchType.LAZY

@OneToMany(mappedBy = "event", fetch = FetchType.LAZY)
private Set<Participation> participations;

除非调用getParticipations(),否则不会加载该关系。

相关问题