如何使用@OneToMany字段和复合主字段实现JPA插入/更新实体的实体

xmjla07d  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(153)

我无法插入/更新MaterialEntity。但我可以findAll()findById()
有人能支持我吗?

@Entity
@Table(name = "material")
public class MaterialEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "code")
    private String code;

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

    @Column(name = "category_id")
    private Integer categoryId;

    @Column(name = "unit_id")
    private Integer unitId;

    @Column(name = "transfer_unit_id")
    private Integer transferUnitId;

    @Column(name = "barcode_type_id")
    private Integer barcodeTypeId;

    @Column(name = "quantity")
    private Integer quantity;

    @Column(name = "description")
    private String description;

    @OneToMany
    @JoinColumn(name="material_id")
    private List<MaterialImageEntity> materialImageEntityList;

    @CreatedDate
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @Column(name = "deleted_at")
    private LocalDateTime deletedAt;

    @Column(name = "created_by")
    private Integer createdBy;

    @Column(name = "updated_by")
    private Integer updatedBy;
}
@Entity
@IdClass(MaterialImageCompositePrimaryKeyEntity.class)
@Table(name = "material_image")
public class MaterialImageEntity {

    @Id
    @Column(name = "material_id", nullable = false)
    private Integer materialId;

    @Id
    @Column(name = "image_id", nullable = false)
    private Integer imageId;

    @CreatedDate
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
}
tf7tbtn2

tf7tbtn21#

尝试手动设置内部列表,以及之前的所有字段:

MaterialEntity materialEntity = new MaterialEntity();

MaterialImageEntity image = new MaterialImageEntity();

List<MaterialImageEntity> materialImageEntityList = new List<MaterialImageEntity>();

iterate ...
materialImageEntityList.add(image)
iterate ...

materialEntity.setMaterialImageEntityList(materialImageEntityList);

....

MaterialEntityRepository.Save(materialEntity);

相关问题