Java 休眠加速插入
插入到数据库需要很多时间,如何加快整个过程?在这个项目中,我使用的是hibernateJava 休眠加速插入,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,插入到数据库需要很多时间,如何加快整个过程?在这个项目中,我使用的是hibernate for (int a = 0; a < persons.size(); a++) { Person person = new Person(); Gender gender = new Gender(); gender.setName(persons.get(a).getGender()); gender = genderRepository.save(gend
for (int a = 0; a < persons.size(); a++) {
Person person = new Person();
Gender gender = new Gender();
gender.setName(persons.get(a).getGender());
gender = genderRepository.save(gender);
Country country = new Country();
country.setName(persons.get(a).getCountry());
country = countryRepository.save(country);
person.setName(personss.get(a).getFirstName());
person.setLastName(persons.get(a).getLastName());
person.setAdditionalInfo(persons.get(a).getIdentifier());
person.setGender(gender);
Set<Country> countries = new HashSet();
countries.add(country);
person.setCountries(countries);
personRepository.save(person);
}
for(inta=0;a
意见:
意见:
您需要在一个hibernate事务中执行所有插入操作。 在Person存储库中创建addPersonList方法
@Autowired
private EntityManager em;
@Transactional
public void addPersonList(){
for (Person personFromList : persons) {
Person person = new Person();
Gender gender = new Gender();
gender.setName(personFromList.getGender());
em.persist(gender);
Country country = new Country();
country.setName(personFromList.getCountry());
em.persist(country);
person.setName(personFromList.getFirstName());
person.setLastName(personFromList.getLastName());
person.setAdditionalInfo(personFromList.getIdentifier());
person.setGender(gender);
Set<Country> countries = new HashSet();
countries.add(country);
person.setCountries(countries);
em.persist(person);
}
}
您需要在一个hibernate事务中执行所有插入操作。 在Person存储库中创建addPersonList方法
@Autowired
private EntityManager em;
@Transactional
public void addPersonList(){
for (Person personFromList : persons) {
Person person = new Person();
Gender gender = new Gender();
gender.setName(personFromList.getGender());
em.persist(gender);
Country country = new Country();
country.setName(personFromList.getCountry());
em.persist(country);
person.setName(personFromList.getFirstName());
person.setLastName(personFromList.getLastName());
person.setAdditionalInfo(personFromList.getIdentifier());
person.setGender(gender);
Set<Country> countries = new HashSet();
countries.add(country);
person.setCountries(countries);
em.persist(person);
}
}
这是我的观点,希望能帮助你