本文介绍了JPA查询获取整棵树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
@Table

我有一个模拟所有类别的类,它们可以按层次排序。 (name =categories)
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =sequence)
@SequenceGenerator(name =序列,sequenceName =categories_pk_seq,allocationSize = 1)
@Column(name =id)
private Long id;

@Column
私人字符串名称;

@OneToOne
@JoinColumn(name =idfather)
私人类别父亲;


$ / code>

我需要分层次排列所有类别父亲跟随其子女和父亲在每个级别上按字母顺序排列),因为它们可以通过oracle中的PRIOR进行创建。是否有可能通过JPA查询(不是SQL的)来实现这一点?

谢谢。

解决方案

简短的答案是;没有没有一个标准的方式来做到这一点。



您必须使用本地sql。



您可能能够扩展Oracle Hibernate Dialect并添加一些用户函数/扩展来获取hibernate来生成PRIOR或CONNECT BY子句,但这会阻止你的应用程序严格遵守JPA和数据库独立的规定。


I have a class which models all categories and they can be ordered hierarchically.

@Entity
@Table(name="categories")
public class Category {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
    @SequenceGenerator(name="sequence", sequenceName="categories_pk_seq", allocationSize=1)
    @Column(name="id")
    private Long id;

    @Column
    private String name;

    @OneToOne
    @JoinColumn(name="idfather")
    private Category father;

}

I need to get all categories ordered hierarchically (I mean every father followed by its children and fathers ordered alphabetically on each level) as they could be made for example with PRIOR in oracle. Is it possible to do this with a JPA Query (not a SQL one)?

Thanks.

解决方案

The short answer is; no there isn't a standard way to do this.

You have to use native sql.

You may be able to extend the Oracle Hibernate Dialect and add some user function/extension to get hibernate to generate PRIOR or CONNECT BY clauses, but this will prevent your app from being strict JPA and database independent.

这篇关于JPA查询获取整棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 19:51