gpt4 book ai didi

java - 如果 NULL 值如何忽略更新或插入连接

转载 作者:行者123 更新时间:2023-12-02 05:45:31 25 4
gpt4 key购买 nike

我有一个实体 bean,其关系 @ManyToOne 位于一列的连接中。

@Entity
@Table(name = "work_order")
public class WorkOrder implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@NotNull
@Column(name = "id_order", nullable = false)
private String idOrder;

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

@Enumerated(EnumType.STRING)
@Column(name = "status")
private StatusOrder status;

@Column(name = "creation_date")
private Instant creationDate;

@Column(name = "closing_date")
private Instant closingDate;

@Column(name = "client_id")
private Long clientId;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) @NotFound (action = NotFoundAction.IGNORE)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "account", insertable = false, updatable = false, nullable = true)
private AnagraficaClienti account;

第二个实体

@Entity
@Table(name = "es_account")
public class AnagraficaClienti implements Serializable {

private static final long serialVersionUID = 1L;


// da rimettere a @NotNull
@Column(name = "fk_cod_azienda", nullable = true)
private String fk_cod_azienda;
@Id
@NotNull
@Column(name = "account", nullable = false)
private String account;
// da rimettere a @NotNull
@Column(name = "tipo_cli_for", nullable = true)
private String tipoClienteFornitore;

@Column(name = "tipo_account", nullable = true)
private String tipoAccount;
....

“es_account”表有三个不可为 null 的主键(fk_cod_azienda、account、tipo_cli_for),与“work_order”表的关系是按 account 列进行的。

我的问题是,有时用户可能会使用空帐户值插入或更新 WorkOrder,而 AnagraficaClienti 实体无法避免这种情况,因为它需要非空(且不重复)值。

当帐户为空时,是否有任何可能的方法可以绕过 AnagraficaClienti 的连接?

最佳答案

在我看来,@ManyToOne 由于创建了冗余关系而违反了 OOP 设计原则。相反,我总是创建一个带有相关实体列表的 @OneToMany 关系。要将关系指定为可为空,只需在 @JoinColumn 中添加 nullable=true 属性即可。使用@ManyToOne,您必须指定属性可选=true。让我们尝试看看它是否有效。

工单

@Entity
@Table(name = "work_order")
public class WorkOrder implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@NotNull
@Column(name = "id_order", nullable = false)
private String idOrder;

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

@Enumerated(EnumType.STRING)
@Column(name = "status")
private StatusOrder status;

@Column(name = "creation_date")
private Instant creationDate;

@Column(name = "closing_date")
private Instant closingDate;

@Column(name = "client_id")
private Long clientId;

AnagraficaClienti

@Entity
@Table(name = "es_account")
public class AnagraficaClienti implements Serializable {

private static final long serialVersionUID = 1L;


// da rimettere a @NotNull
@Column(name = "fk_cod_azienda", nullable = true)
private String fk_cod_azienda;
@Id
@NotNull
@Column(name = "account", nullable = false)
private String account;
// da rimettere a @NotNull
@Column(name = "tipo_cli_for", nullable = true)
private String tipoClienteFornitore;

@Column(name = "tipo_account", nullable = true)
private String tipoAccount;

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name = "account", nullable = true)
private List<WorkOrder> workOrders;

当您要将工单插入数据库时​​:

workOrderRepository.save(workOrder);

当您想要创建关系时:

AnagraficaClienti client = anagraficaClientiRepository.findById(...);
client.getWorkOrders().add(newWorkOrder);

关于java - 如果 NULL 值如何忽略更新或插入连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com