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

Refactor getSingleResult() to getResultList() #536

Open
yvespp opened this issue Mar 6, 2020 · 3 comments
Open

Refactor getSingleResult() to getResultList() #536

yvespp opened this issue Mar 6, 2020 · 3 comments

Comments

@yvespp
Copy link
Member

yvespp commented Mar 6, 2020

We should refactor code that calls getSingleResult() to getResultList() where the NoResultException is caught and it's ok if there is no result. See https://stackoverflow.com/questions/24045342/why-jpa-uses-javax-persistence-noresultexception

If the NoResultException is not caught directly in the method it can unintentionally lead to transaction rollbacks. See: http://glassfish.10926.n7.nabble.com/Unexpected-Behavior-NoResultException-rolls-back-transaction-td20819.html

I'm not sure what the best way to refactor it is:

@yvespp yvespp changed the title Refactor getSingleResult() to getSingleResult() Refactor getSingleResult() to getResultList() Jun 12, 2020
@mburri
Copy link
Member

mburri commented Jun 26, 2020

What about returning Optionals from the repositories?

    public String getResourceName(int resourceId) throws ResourceNotFoundException {
        ResourceEntity resourceEntity = resourceRepository.find(resourceId);
        if (resourceEntity == null) {
            throw new NoResultException("No Resource with id " + resourceId);
        }
        return resourceEntity.getName();
    }
}

would become:

        public String getResourceName(int resourceId) {
               return resourceRepository.find(resourceId)
                                 .map(ResourceEntity::getName)
                                 .orElseThrow(() -> new NoResultException("No Resource with id " + resourceId));
    }

@yvespp
Copy link
Member Author

yvespp commented Jun 29, 2020

You mean returning optionals (your example doesn't do that)? Sure that could be another way.
For that all the method calls would have to be refactored and you still would have to map the optional to a http status in the rest service explicitly. Also maybe some calls still want an exception when nothing is found.

Maybe we have to split up some code into two methods: one method throws NoResultException and the other doesn't but can return null.

@mburri
Copy link
Member

mburri commented Jun 29, 2020

My example covers the resource, not the repsitory - which would (in this case) return an Optional - yes.
Returning optionals from the respository forces the caller to handle the possibilty that nothing was found - in contrast to the NoResultException thrown by getSingleResult()

Yes - we would have to refactor all calls and do the mapping explicitly - but the nice thing is, that the compiler will tell us about the places where we have to do this - compared to runtime exceptions...

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

2 participants