我有以下自引用实体的代码
@Data
@Builder
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class Dictionary implements Persistable<Long> {
@Id
@GeneratedValue(generator = "dictionary_gen", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "dictionary_gen", sequenceName = "dictionary_item_key_seq", allocationSize = 1)
@Column(name = "item_key", unique = true, updatable = false, columnDefinition = "serial")
private Long itemKey;
@Override
@JsonIgnore
public Long getId() {
return itemKey;
}
@Override
@JsonIgnore
public boolean isNew() {
return itemKey == null;
}
@Nullable
@ManyToOne
@JoinColumn(name = "parent_item_key", columnDefinition = "",
foreignKey = @ForeignKey(name = "dictionary_item_key_fk", value = ConstraintMode.NO_CONSTRAINT))
@JsonIgnore
private Dictionary parent;
@JsonProperty("parentItemKey")
public Long getParentItemKey() {
return parent.getItemKey();
}
@Column(name = "item_name", length = 120, nullable = false)
private String itemName;
@Column(name = "item_description", length = 120, nullable = false)
private String itemDescription;
}
它是按照DDL生成的
create table dictionary (item_key serial not null,
item_description varchar(120) not null,
item_name varchar(120) not null,
parent_item_key serial,
primary key (item_key))
正如您可以看到的,serial 列定义从 item_key 列转换为 parent_item_key。如何将 parent_item_key 定义为非 serial?如果我的字典项没有父项,我想保持它为空。我试过使用@AssociationOverride,但它也没有帮助。
1条答案
按热度按时间zqry0prt1#
我想你可以这样用。
(可选)关联是否可选。如果设置为false,则必须始终存在非空关系。