Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janmisek/e0d6dc30ced052dbe48b650cf6c6ad43 to your computer and use it in GitHub Desktop.
Save janmisek/e0d6dc30ced052dbe48b650cf6c6ad43 to your computer and use it in GitHub Desktop.
Get spring data repository by entity class
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