如果值为NULL,如何忽略更新或插入联接

时间:2019-05-12 12:45:45

标签: java hibernate jpa

我有一个实体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”表具有三个不可为空的主键(fk_cod_azienda,account,tipo_cli_for),并且与“ work_order”表的关系是按“帐户”列。

我的问题是,有时用户可能会使用空帐户值插入或更新WorkOrder,而AnagraficaClienti实体却无法避免,因为它期望一个非空(而不是重复)值。

是否有任何方法可以在帐户为空时绕过AnagraficaClienti进行联接?

1 个答案:

答案 0 :(得分:0)

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

工作单

val

AnagraficaClienti

@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;

要将工作订单插入数据库时​​:

@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);