Сохранение данных в Embeddable таблицу, attempted to assign id from null one-to-one property

пытаюсь сохранить данные в таблицу receipt_has_periodicals в которой есть periodicalId, receiptId и 2 других поля. Реализовал связь при помощи @EmbeddedID и @Embeddable. Вот класс PeriodicalsHasReceiptEntity с @EmbeddedID:

@Table(name = "periodical_has_receipt", indexes = {@Index(name = "fk_periodical_has_receipt_periodical1_idx", columnList = "periodical_sell_id"), @Index(name = "fk_periodical_has_receipt_receipt1_idx", columnList = "receipt_id")})
@Entity
public class PeriodicalHasReceiptEntity {
    @EmbeddedId
    private PeriodicalHasReceiptEntityId id;

    @Column(name = "price_per_month", nullable = false, precision = 4, scale = 2)
    private BigDecimal pricePerMonth;

    @Column(name = "number_of_month", nullable = false, length = 45)
    private Integer numberOfMonth;

    @ManyToOne
    @MapsId("receiptId")
    @JoinColumn(name = "receipt_id")
    ReceiptEntity mReceipt;

    @ManyToOne
    @MapsId("periodicalSellId")
    @JoinColumn(name = "periodical_sell_id")
    PeriodicalEntity periodical;

    *Getters and setters*

Класс PeriodicalsHasReceiptEntityId @Embeddable:

@Embeddable
public class PeriodicalHasReceiptEntityId implements Serializable {

    @Column(name = "periodical_sell_id", nullable = false)
    private Integer periodicalSellId;

    @Column(name = "receipt_id", nullable = false)
    private Integer receiptId;

    *Getters and setters*

И классы entity откуда и берем periodicalId, receiptId.
Класс PeriodicalEntity:

@Table(name = "periodical", indexes = {@Index(name = "idx_periodical_title", columnList = "title"), @Index(name = "fk_periodical_publisher1_idx", columnList = "publisher_id")}, uniqueConstraints = {@UniqueConstraint(name = "title_UNIQUE", columnNames = {"title"})})
@Entity
public class PeriodicalEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "sell_id", nullable = false)
    private Integer sellId;

    @Column(name = "title", nullable = false, unique = true, length = 125)
    private String title;

    @Column(name = "number_of_pages", nullable = false)
    private Integer numberOfPages;

    @Column(name = "periodicity_per_year", nullable = false)
    private Integer periodicityPerYear;

    @Column(name = "percentage_of_advertising", nullable = false)
    private Integer percentageOfAdvertising;

    @Column(name = "price_per_month", nullable = false)
    private BigDecimal pricePerMonth;

    @Column(name = "description", nullable = false)
    private String description;

    @Column(name = "rating")
    private Double rating;

    @ManyToOne(optional = false)
    @JoinColumn(name = "publisher_id", nullable = false)
    private PublisherEntity publisher;

    @Column(name = "images", nullable = false, length = 500)
    private String images;

    @OneToMany(mappedBy = "periodical")
    Set<PeriodicalHasReceiptEntity> receiptEntities;

    @OneToMany(mappedBy = "periodical")
    Set<PeriodicalHasSubjectEntity> periodicalHasSubject;

    *Getters and setters*

Класс ReceiptEntity:

@Table(name = "receipt", indexes = {@Index(name = "fk_receipt_user1_idx", columnList = "user_id"), @Index(name = "fk_receipt_status1_idx", columnList = "status_id")})
@Entity
public class ReceiptEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = false, length = 45)
    private String name;

    @Column(name = "surname", nullable = false, length = 45)
    private String surname;

    @Column(name = "adress", nullable = false, length = 1024)
    private String adress;

    @Column(name = "telephone_number", nullable = false, length = 14)
    private String telephoneNumber;

    @ManyToOne(optional = false)
    @JoinColumn(name = "status_id", nullable = false)
    private StatusEntity status;

    @ManyToOne(optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private UserEntity user;

    @Column(name = "email", nullable = false, length = 100)
    private String email;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time")
    private Date createTime;

    @OneToMany(mappedBy = "mReceipt")
    Set<PeriodicalHasReceiptEntity> receiptEntities;

    *Getters and setters*

И сам метод, в котором я пытаюсь добавить значения в таблицу(здесь же я добавляю новый заказ):

@PostMapping("/buy-now")
    public String buyNow(@RequestParam("periodicalId") Integer periodicalId, HttpServletRequest request){
        UserEntity user = (UserEntity) request.getSession().getAttribute("USER");
        if (user == null){
            return "redirect:/login";
        }
        ReceiptEntity receiptEntity = new ReceiptEntity();
        StatusEntity status = statusService.getStatusById(1);
        receiptEntity.setStatus(status);
        receiptEntity.setAdress(user.getAddress());
        receiptEntity.setTelephoneNumber(user.getTelephone());
        receiptEntity.setEmail(user.getEmail());
        receiptEntity.setName(user.getName());
        receiptEntity.setSurname(user.getSurname());
        receiptEntity.setUser(user);
        Integer receiptId = receiptService.saveReceipt(receiptEntity);
        PeriodicalHasReceiptEntity periodicalHasReceipt = new PeriodicalHasReceiptEntity();
        PeriodicalEntity periodical2 = new PeriodicalEntity();
        periodical2.setSellId(periodicalId);
        periodical2.setPricePerMonth(BigDecimal.valueOf(16));
        PeriodicalHasReceiptEntityId periodicalHasReceiptEntityId = new PeriodicalHasReceiptEntityId();
        periodicalHasReceiptEntityId.setReceiptId(receiptId);
        periodicalHasReceiptEntityId.setPeriodicalSellId(periodicalId);
        periodicalHasReceipt.setId(periodicalHasReceiptEntityId);
        periodicalHasReceipt.setPricePerMonth(periodical2.getPricePerMonth());
        periodicalHasReceipt.setNumberOfMonth(1);

        periodicalHasReceiptService.saveOrder(periodicalHasReceipt);
        return "redirect:/shop";
    }

Использую методы CrudRepository для сохранения данных. Заказ в БД добавляется, а вот информация в таблицу receipt_has_periodicals влечет за собой ошибку:

attempted to assign id from null one-to-one property [com.periodical.trots.entities.PeriodicalHasReceiptEntity.periodical]

Помогите разобраться, в чем же дело.


Ответы (0 шт):