繁体   English   中英

如何使用 Mutiny UNI Quarkus reactive 实现递归方法 sql Java

[英]How to implement recursive method using Mutiny UNI Quarkus reactive sql Java

我有一个 Person 表,其中包含以下信息。

人员表 类型
人员编号 情报局
姓名 可变字符
父亲身份 INT --引用同表中的personId(Person)
妈妈号 INT --引用同表中的personId(Person)
更多专栏 其他详情

我必须使用返回家谱的 ASYN 编程来实现类似于下面旧实现的方法。

较早的实施

我的POJO class

public class FamilyTree

    Person person;
    FamilyTree fatherFamily;
    FamilyTree motherFamily;
    
    public FamilyTree (Person person, FamilyTree father, FamilyTree mother){
        this.person = person;
        this.fatherFamily = father;
        this.motherFamily = mother;
    }
    
    public static FamilyTree buildFamilyTree(int personId){
        Person person = PersonRepository.GetPersonById(personId);
        FamilyTree fatherTree = (person.getFatherId == null || person.getFatherId(isEmpty())?        null:buildFamilyTree(person.getFatherId());
        FamilyTree motherTree = (person.getMotherId == null || person.getMotherId(isEmpty())?null:buildFamilyTree(person.getMotherId());
        return new FamilyTree(person, fatherTree, motherTree);
    }

我如何使用 Mutiny 和 Quarkus reactive SQL 实现这个而不导致块 IO 异常?

我需要的新实现 class 是:

@ApplicationScoped
public class FamilyTreeRepostiory{

    @Inject
    OraclePool client;
    
    public Uni<Person> getPersonById(int personId){
        String sql = "select * from person where personId=?";
        Tuple tuple = Tuple.of(personId);
        return client.preparedQuery(sql).execute(tuple).onItem().transform(Rowset::iterator)
           .onItem.transform(iterator-> iterator.hasNext()?Person.convertPerson(iterator.next()):null);
    }
    
    public Uni<FamilyTree> getFamilyTree(int personId){
        Uni<Person> person = getPersonById(personId);
        //help with this implementation is needed.
        return familyTree ;
    }
}

使用 Mutiny 和 Quarkus reactive SQL 的 getFamilyTree() 方法的实现可能如下所示:

public Uni<FamilyTree> getFamilyTree(int personId){
    Uni<Person> person = getPersonById(personId);
    return person.flatMap(p -> Uni.combine().all(
        getFamilyTree(p.getFatherId()),
        getFamilyTree(p.getMotherId())
    ).map(tuple -> new FamilyTree(p, tuple.getValue1(), tuple.getValue2()))); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM