Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Hibernate中通过递归oracle查询获取root_Java_Sql_Oracle_Hibernate - Fatal编程技术网

Java 如何在Hibernate中通过递归oracle查询获取root

Java 如何在Hibernate中通过递归oracle查询获取root,java,sql,oracle,hibernate,Java,Sql,Oracle,Hibernate,我有点好奇,有没有一种方法可以像根实体一样通过sql查询获得connect的结果,并且已经映射了子体。 所以,如果我在base中插入类似这样的内容: insert into table test (id, parent_id, some_text) values (1, null, 'a'), (2, 1, 'b'), (3, 1, 'c'), (4, 2, 'd'); 然后通过sql查询 select * from test t start with t.id = 1 connect by

我有点好奇,有没有一种方法可以像根实体一样通过sql查询获得connect的结果,并且已经映射了子体。 所以,如果我在base中插入类似这样的内容:

insert into table test (id, parent_id, some_text) values
(1, null, 'a'),
(2, 1, 'b'),
(3, 1, 'c'),
(4, 2, 'd');
然后通过sql查询

select *
from test t
start with t.id = 1
connect by prior t.id = t.parent_id
order siblings by t.some_text
我会得到

id | parent_id | some_text
 1        null           a
 2           1           b
 4           2           d
 3           1           c
按实体分类

@Entity
@Table(name = "test")
public class Test {
    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    private BigInteger id;

    @Column(name = "parent_id")
    private BigInteger parent_id;

    @Column(name = "some_text")
    private String someText;

    @OneToMany(mappedBy = "parent")
    private Set<Test> descendants;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Test parent;
    // getters and setters
}
@实体
@表(name=“test”)
公开课考试{
@身份证
@列(name=“id”)
@生成值(生成器=“增量”)
@GenericGenerator(name=“increment”,strategy=“increment”)
私有整数id;
@列(name=“parent\u id”)
私有BigInteger父\u id;
@列(name=“some_text”)
私有字符串someText;
@OneToMany(mappedBy=“家长”)
私有集后代;
@许多酮
@JoinColumn(name=“parent\u id”)
私人测试父母;
//接球手和接球手
}
它将作为测试列表返回给我。通过递归函数可以得到根和完整的树,但它会在迭代中得到新的查询(如果我有一棵大树,它会很长)。
因此,有没有一种可能的好方法可以通过这个查询(可能扩展/实现一些类/接口,这些类/接口将处理从jdbc到实体的映射)获得具有已映射子体的树的根呢?

您可以使用
connectbyroot
一元运算符。 看


顺便说一句:这与冬眠无关。这是纯粹针对Oracle的解决方案。

thx供您回答,但Hibernate这是主要问题,我将尝试将您的查询与addRoot()一起使用,如果可以的话,我在这里告诉您。
select t.*, connect_by_root id
from test t
start with t.id = 1
connect by prior t.id = t.parent_id
order siblings by t.some_text