Ask Your Question
3

Can the commit logic in Javers be modified to allow for an EntityDefinition parameter?

asked 2023-02-11 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2022-01-26 04:00:00 +0000

woof gravatar image

Yes, it is possible to modify the commit logic in Javers to allow for an EntityDefinition parameter.

Javers provides a pre-commit hook mechanism that allows you to modify the changes before they are persisted. The pre-commit hook can be registered using the JaversBuilder like this:

Javers javers = JaversBuilder.javers()
                    .registerJaversRepositoryEventListener(new MyPreCommitHook())
                    .build();

In the implementation of the MyPreCommitHook, you can access the changes and modify them as required. You can also get the EntityDefinition for the affected entities and use it to modify the commit logic.

Here’s an example implementation of the MyPreCommitHook that modifies the commit logic based on the EntityDefinition:

class MyPreCommitHook implements JaversRepositoryEventListener {
    @Override
    public void onCommit(CommitMetadata commitMetadata) {
        // get the changes for the current commit
        List<Change> changes = JaversContext.getCachedChanges();

        // iterate over the changes and modify them as needed
        for (Change change : changes) {
            EntityDefinition entityDefinition = JaversContext.getEntityDefinition(change.getAffectedGlobalId());

            if (entityDefinition.getName().equals("MyEntityType")) {
                // modify the commit logic for MyEntityType
            }
        }
    }
}

In this example, we are iterating over the changes and checking if the affected entity is of type MyEntityType. If it is, we can modify the commit logic accordingly. The JaversContext.getEntityDefinition() method provides us with the EntityDefinition for the affected entity, which we can use to get more information about the entity, such as its properties and relationships.

Overall, this approach allows for more fine-grained control over the commit logic in Javers, and enables you to customize how changes are persisted based on the entity type or other criteria.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-11 11:00:00 +0000

Seen: 8 times

Last updated: Jan 26 '22