0

我正在使用 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 个额外的选择。

我怎样才能改变这种休眠行为?

4

1 回答 1

0

如果您想最小化 Hibernate 开销,为什么不在@NamedNativeQuery实体中定义您的自定义或直接使用createNativeQuery(sqlString, Category.class)?ORM 很有用,但它们对于复杂的查询/表结构也不是很有效。

于 2016-04-05T12:23:37.927 回答