Created
May 2, 2019 11:25
-
-
Save janmisek/e0d6dc30ced052dbe48b650cf6c6ad43 to your computer and use it in GitHub Desktop.
Get spring data repository by entity class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Repository getRepository() { | |
String domain = ((Unique) annotation).domain(); | |
try { | |
Repositories repositories = new Repositories(listableBeanFactory); | |
Class domainClass = Class.forName(domain); | |
Optional repo = repositories.getRepositoryFor(domainClass); | |
if (repo.isPresent()) { | |
return (Repository) repo.get(); | |
} else { | |
throw new RuntimeException("Repository for " + domain + " not found"); | |
} | |
} catch (Exception e) { | |
throw new RuntimeException("Cannot find repository for domain " + domain); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment