Results 1 to 2 of 2

Thread: How to use Repository Populator

Hybrid View

  1. #1
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default 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. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •