简体   繁体   中英

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

I have an Person table which contains below information.

Person table type
personId INT
Name VarChar
fatherId INT --refer to personId in same table(Person)
MotherId INT --refer to personId in same table(Person)
More columns other details

I have to implement a method similar to the below older implementation below using ASYN programming that returns a family tree.

Older Implementation

My 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);
    }

How do I implement this with Mutiny and Quarkus reactive SQL without causing block IO exceptions?

New implementation class of what I need is:

@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 ;
    }
}

The implementation of the getFamilyTree() method using Mutiny and Quarkus reactive SQL could look like this:

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()))); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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