[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get paginated results when fetching changes on multiple types? #1268

Open
thinow opened this issue Feb 28, 2023 · 5 comments
Open

How to get paginated results when fetching changes on multiple types? #1268

thinow opened this issue Feb 28, 2023 · 5 comments

Comments

@thinow
Copy link
thinow commented Feb 28, 2023

How to get the changes for multiple entity types ? Those types are all audited by Javers, and have one-to-many relationships. Furthermore, the search needs to be paginated (because of the big amount of data).

Let's consider the first type Hotel with a one-to-many relationship to Room. Hotel has a second one-to-many relationship to Recommendation.

Currently, the search looks like the following. This implementation is not efficient since it fetches all data before reducing the result set to display a page.

// search by instance
def searchForChanges(instance) {
    JqlQuery query = QueryBuilder.byInstance(instance)
            .withNewObjectChanges()
            .withChildValueObjects()
            .build();
    return javers.findChanges(query);    
}


Hotel hotel = new Hotel(uuid, "My Hotel");


List<Changes> changes = new ArrayList()

// search the changes for the hotel
changes.addAll(searchForChanges(hotel))

// repeat the search for each room
for (room in hotel.getRooms()) {
    changes.addAll(searchForChanges(room))
}

// repeat the search for each recommendation
for (recommendation in hotel.getRecommendation()) {
    changes.addAll(searchForChanges(recommendation))
}

// then sort, and build a page
changes.sort(comparator)
        .stream()
        .limit(...)
        .skip(...)

Example of the entities:

@Entity
@Table(name = "hotel")
public class Hotel {

    @Id
    private UUID id;
    private String name;

    @DiffIgnore
    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
    @JoinColumn(name = "hotelId")
    private List<Room> rooms = new LinkedList<>();

    @DiffIgnore
    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
    @JoinColumn(name = "hotelId")
    private List<Recommendation> recommendations = new LinkedList<>();
}

@Entity
@Table(name = "Room")
public class Room {

    @Id
    private UUID id;
    private UUID hotelId;
    private int number;
}

@Entity
@Table(name = "Recommendation")
public class Recommendation {

    @Id
    private UUID id;
    private UUID hotelId;
    private int stars;
    private String comment;
}

Expected results: a list of changes ordered by date made of different types :

change #10: Hotel changed its name
change #9: Room changed its number
change #8: Recommendation was added to the hotel
change #7: A room was added to the hotel
change #6: Hotel was created
        (The following changes are available on page 2)

Side-note:
I'm aware of the methods limit and skip on the class QueryBuilder, but since I need to execute the search by instance, it won't paginate the results of multiple instance types.

@bartoszwalacik
Copy link
Member

Hi @thinow. Is this a feature request? Are you proposing a new query method, sth like this ?

QueryBuilder.byInstances(List<?> instances)

@thinow
Copy link
Author
thinow commented Mar 17, 2023

Hi @bartoszwalacik,

Sorry for the delayed answer.

If the latest version of javers does not support pagination on multiple instance types, then it is probably a feature request. Assuming the javers supports byInstances(List<?> instances), would it also provide the skip and limit methods to paginate?

@bartoszwalacik
Copy link
Member

Javers supports pagination (skip and limit) for all queries

@Akayeshmantha
Copy link
Akayeshmantha commented May 13, 2023

Hello I am having the same issue.
Is ther a workaround to achieve below with a custome jql query ?

QueryBuilder.byInstances(List<?> instances)

@bartoszwalacik
Copy link
Member
bartoszwalacik commented May 17, 2023

@thinow @Akayeshmantha implementing QueryBuilder.byInstances(List<?> instances) in Javers seems like a good idea for me. It's a mid size task, but the reality here is if you need it -- you contribute a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants