Advertisement
BapBapuHa

DataLoader

Apr 12th, 2022
1,210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package com.example.pds.controllers.profiles;
  2.  
  3. import com.example.pds.model.roles.RoleRepository;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.CommandLineRunner;
  6. import org.springframework.core.annotation.Order;
  7.  
  8. @Order(1)
  9. public class ProfileDataLoader implements CommandLineRunner {
  10.  
  11.     @Autowired
  12.     ProfilesRepository profilesRepository;
  13.  
  14.     @Autowired
  15.     RoleRepository roleRepository;
  16.  
  17.     @Override
  18.     public void run(String... args) throws Exception {
  19.         loadProfileData();
  20.     }
  21.  
  22.     void loadProfileData() {
  23.         if (profilesRepository.count() == 0) {
  24.  
  25.             Profile profile1 = new Profile();
  26.             profile1.setUsername("Asparuh");
  27.             profile1.setPassword("1234");
  28.             profile1.setEmail("Email");
  29.             profile1.setRole(roleRepository.findRoleById(1));
  30.  
  31.             Profile profile2 = new Profile();
  32.             profile1.setUsername("Artur");
  33.             profile1.setPassword("1234");
  34.             profile1.setEmail("OtherEmail");
  35.             profile1.setRole(roleRepository.findRoleById(2));
  36.  
  37.             Profile profile3 = new Profile();
  38.             profile1.setUsername("Spiridon");
  39.             profile1.setPassword("1234");
  40.             profile1.setEmail("EmailEmail");
  41.             profile1.setRole(roleRepository.findRoleById(3));
  42.  
  43.             Profile profile4 = new Profile();
  44.             profile1.setUsername("Margarit");
  45.             profile1.setPassword("1234");
  46.             profile1.setEmail("SomeEmail");
  47.             profile1.setRole(roleRepository.findRoleById(4));
  48.  
  49.             profilesRepository.saveAndFlush(profile1);
  50.             profilesRepository.saveAndFlush(profile2);
  51.             profilesRepository.saveAndFlush(profile3);
  52.             profilesRepository.saveAndFlush(profile4);
  53.         }
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement