Announcement Announcement Module
Collapse
No announcement yet.
How to use Repository Populator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use Repository Populator

    I'm wondering how others are using the repository populators.
    I've looked into the code and the 'default' ResourceReaderRepositoryPopulator executes which will overwrite existing data...

    I was thinking of extending the AbstractRepositoryPopulatorFactoryBean but this one is really tied to using Resources.
    And hence the protected abstract ResourceReader getResourceReader(); which need to be implemented.

    I'm basically looking for a Code-based RepositoryPopulator which code be done easily by implementing the RepositoryPopulator.
    The populate method will in this case call directly the repository and do 'stuff'. E.g. checking if records exists, and *only* if not create them.
    But the problem to what class assign this populator then.

    I can create a e.g. a SimpleRepositoryPopulatorFactoryBean which is just a copy of AbstractRepositoryPopulatorFactoryBean without Resource related things. It would accept one ore more RepositoryPopulator which populate method would be called.

    What do you think about this.



    Note that I'm currently using similar code to populate my repositories:

    Code:
    public class BootStrap implements ApplicationListener<ContextRefreshedEvent> {
    
    	private static Log LOG = LogFactory.getLog(BootStrap.class);
    
    	@Inject
    	private UserRepository userRepository;
    
    	@Override
    	public void onApplicationEvent(ContextRefreshedEvent event) {
    		LOG.info("Bootstrapping application...");
    		if (userRepository.count() == 0) {
    			LOG.info("User repository is empty, creating default admin user...");
    			User admin = new User(..);
    			userRepository.saveAndFlush(admin);
    		}
    	}
    }
    But when I read about the RepositoryPopulator in Spring Data Commons I thought it would be much cleaner to migrate my custom class and implement the RepositoryPopulator. Much cleaner code, as the ApplicationListener<ContextRefreshedEvent> is off course very generic.

  • #2
    The sole purpose of the repository populators is to read data from resources and populating a repo with them. Think of them as an analogy of the capability to populate a DataSource with scripts using the jdbc namespace, just that we're essentially removing the store specific data definition (SQL) and move to an independent one (JSON, XML). Thus they are usually targeted to be used with in-memory stores or stores that are cleaned up on JVM shut down.

    For general purpose initialization, your approach using the ApplicationListener is just fine.

    Comment

    Working...
    X