Skip to content

Instantly share code, notes, and snippets.

@alamnr
Forked from btforsythe/ReviewApplication.java
Created November 22, 2018 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alamnr/089c943395eb448478d2c8c1a104d60c to your computer and use it in GitHub Desktop.
Save alamnr/089c943395eb448478d2c8c1a104d60c to your computer and use it in GitHub Desktop.
Example of populating temporary database with Spring Boot + JPA for WCCI Review Site exercise.
package review;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class ReviewApplication {
public static void main(String[] args) {
SpringApplication.run(ReviewApplication.class, args);
}
@Resource
private ReviewRepository reviewRepository;
@Bean
public CommandLineRunner populateReviews() {
return new ReviewPopulatorRunner();
}
public class ReviewPopulatorRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
/*
* Since it's the first Review being created, the generated id for this Review will be 1 (specifically, 1L since it's long).
*/
reviewRepository.save(new Review("title", "author", "content", new Date()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment