英文:
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 = "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;
ProductLinkBoxes.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;
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论