我正在使用 Spring Data JPA + Postgresql,并且我有实体“Category”,它实现了 Postgresql LTree 类型,如下所示:
@Id
@SequenceGenerator(name = "categories_id_seq", sequenceName = "categories_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "categories_id_seq")
@Column(name = "id", updatable = false)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "title")
private String title;
@Column(name = "trail")
@Type(type = "ltree")
private String trail;
@Column(name = "layer")
private Integer layer;
@Column(name = "parent_id")
private Integer parentId;
@ManyToOne()
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
private Category parent;
@OneToMany
@Fetch(FetchMode.JOIN)
@BatchSize(size = 20)
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
private Set<Category> childrens;
我的问题是当我想将整个子树从给定节点加载到“叶”节点时,Hibernate 为每对父节点和子节点发出 2 个额外的选择。
我怎样才能改变这种休眠行为?