能够一次合并多个未持久化的子实体吗?

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

Is it possible to merge multiple non persisted child entities at once?

问题

我正在尝试通过父实体的级联类型将多个新实体保存到数据库。结构如下:

Machine(在此定义级联类型ALL)

  • ProductLinkBox

ProductLinkBox(在此定义级联类型ALL)

  • ProductLinkMetaData

ProductLinkMetaData(在此定义级联类型ALL)

  • FirmwareMetaData

最后,我在机器对象上调用MachineRepository.save。Machine和ProductLinkBox对象成功持久化,但其他实体没有。我是否需要手动持久化所有其他类型?Hibernate是否无法自动持久化所有其他父子关系?

以下是您提供的Java代码的翻译:

Machine.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "TAB_MACHINE")
public class Machine extends AbstractEntity {

  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "machineIdGenerator")
  @SequenceGenerator(
      name = "machineIdGenerator",
      sequenceName = "SEQ_MACHINE_ID",
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @OneToMany(cascade = CascadeType.ALL, mappedBy = "machine", fetch = FetchType.LAZY, orphanRemoval = true)
  private List<ProductLinkBox> productLinkBoxes;
}

ProductLinkBox.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "TAB_PRODUCT_LINK_BOX")
public class ProductLinkBox extends AbstractEntity {

  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "productLinkBoxIdGenerator")
  @SequenceGenerator(
      name = "productLinkBoxIdGenerator",
      sequenceName = "SEQ_PRODUCT_LINK_BOX_ID",
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @ManyToOne
  @JoinColumn(name = "MACHINE_ID")
  private Machine machine;

  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "productLinkBox")
  private ProductLinkMetaData productLinkMetaData;
}

ProductLinkMetaData.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "TAB_PRODUCT_LINK_META_DATA")
public class ProductLinkMetaData extends AbstractEntity {

  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "productLinkMetaDataIdGenerator")
  @SequenceGenerator(
      name = "productLinkMetaDataIdGenerator",
      sequenceName = "SEQ_PRODUCT_LINK_META_DATA_ID",
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "PRODUCT_LINK_BOX_ID")
  @MapsId
  private ProductLinkBox productLinkBox;

  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "productLinkMetaData")
  private FirmwareMetaData firmwareMetaData;
}

FirmwareMetaData.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "TAB_FIRMWARE_META_DATA")
public class FirmwareMetaData extends AbstractEntity {

  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "firmwareMetaDataIdGenerator")
  @SequenceGenerator(
      name = "firmwareMetaDataIdGenerator",
      sequenceName = "SEQ_FIRMWARE_META_DATA_ID",
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "PRODUCT_LINK_META_DATA_ID")
  @MapsId
  private ProductLinkMetaData productLinkMetaData;
}

问题已经通过遵循Vlad Mihalcea的一对一映射说明以及他的GitHub链接中的代码解决。

英文:

I am trying to save multiple new entities to the database via a parent entity cascadeType. The structure is as follows:

Machine (cascadeType ALL defined here)

  • ProductLinkBox

ProductLinkBox (cascadeType ALL defined here)

  • ProductLinkMetaData

ProductLinkMetaData (cascadeType ALL defined here)

  • FirmwareMetaData

In the end I call MachineRepository.save on the machine object. The Machine and ProductLinkBox object is successfully persisted, but nothing else. Do I need to manually persist all of the other types as well? Is hibernate not able to automatically persist all of the other parent - child relations?

Machine.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = &quot;TAB_MACHINE&quot;)
public class Machine extends AbstractEntity {

  @Id
  @Column(name = &quot;ID&quot;)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;machineIdGenerator&quot;)
  @SequenceGenerator(
      name = &quot;machineIdGenerator&quot;,
      sequenceName = &quot;SEQ_MACHINE_ID&quot;,
      allocationSize = 1,
      initialValue = 10)
  Long id;


  @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;machine&quot;, fetch = FetchType.LAZY, orphanRemoval = true)
  private List&lt;ProductLinkBox&gt; productLinkBoxes;

ProductLinkBoxes.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = &quot;TAB_PRODUCT_LINK_BOX&quot;)
public class ProductLinkBox extends AbstractEntity {

  @Id
  @Column(name = &quot;ID&quot;)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;productLinkBoxIdGenerator&quot;)
  @SequenceGenerator(
      name = &quot;productLinkBoxIdGenerator&quot;,
      sequenceName = &quot;SEQ_PRODUCT_LINK_BOX_ID&quot;,
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @ManyToOne
  @JoinColumn(name = &quot;MACHINE_ID&quot;)
  private Machine machine;

  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = &quot;productLinkBox&quot;)
  private ProductLinkMetaData productLinkMetaData;

ProductLinkMetaData.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = &quot;TAB_PRODUCT_LINK_META_DATA&quot;)
public class ProductLinkMetaData extends AbstractEntity {

  @Id
  @Column(name = &quot;ID&quot;)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;productLinkMetaDataIdGenerator&quot;)
  @SequenceGenerator(
      name = &quot;productLinkMetaDataIdGenerator&quot;,
      sequenceName = &quot;SEQ_PRODUCT_LINK_META_DATA_ID&quot;,
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = &quot;PRODUCT_LINK_BOX_ID&quot;)
  @MapsId
  private ProductLinkBox productLinkBox;

  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = &quot;productLinkMetaData&quot;)
  private FirmwareMetaData firmwareMetaData;

FirmwareMetaData.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = &quot;TAB_FIRMWARE_META_DATA&quot;)
public class FirmwareMetaData extends AbstractEntity {

  @Id
  @Column(name = &quot;ID&quot;)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;firmwareMetaDataIdGenerator&quot;)
  @SequenceGenerator(
      name = &quot;firmwareMetaDataIdGenerator&quot;,
      sequenceName = &quot;SEQ_FIRMWARE_META_DATA_ID&quot;,
      allocationSize = 1,
      initialValue = 10)
  Long id;

  @OneToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = &quot;PRODUCT_LINK_META_DATA_ID&quot;)
  @MapsId
  private ProductLinkMetaData productLinkMetaData;

EDIT: Solved by following Vlad Mihalceas OneToOne mapping instructions in combination with the code from his github link.

答案1

得分: 1

已按照Vlad Mihalcea的OneToOne映射指南结合他的GitHub链接中的代码进行解决。从实体中删除不必要的ID映射并使用共享的主键(PK)就解决了问题。

英文:

Solved by following Vlad Mihalceas OneToOne mapping instructions in combination with the code from his github link. Removing the unnecessary ID mappings from the entities and using a shared PK did the trick.

huangapple
  • 本文由 发表于 2023年3月31日 21:38:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899194.html
匿名

发表评论

匿名网友

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

确定