Last active
November 22, 2018 11:22
-
-
Save btforsythe/e5555d0db83b72f65f8077dbec378ece to your computer and use it in GitHub Desktop.
Example of populating temporary database with Spring Boot + JPA for WCCI Review Site exercise.
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
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