Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot springboot应用程序:未在数据库中创建实体_Spring Boot_Jpa - Fatal编程技术网

Spring boot springboot应用程序:未在数据库中创建实体

Spring boot springboot应用程序:未在数据库中创建实体,spring-boot,jpa,Spring Boot,Jpa,我使用springboot应用程序,并尝试在数据库中插入35000条记录 这是主课 @SpringBootApplication @EnableTransactionManagement public class DemoImmobilierBackApplication implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(DemoImm

我使用springboot应用程序,并尝试在数据库中插入35000条记录

这是主课

@SpringBootApplication
@EnableTransactionManagement
public class DemoImmobilierBackApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(DemoImmobilierBackApplication.class, args);
    }
这是服务

@Service
public class ReferenceServiceImpl implements ReferenceService {
    
    Logger logger = LoggerFactory.getLogger(ReferenceServiceImpl.class);
    
    @Autowired
    private ReferenceRepository referenceRepository;
    
    
    @Transactional
    public void loadDataBaseCodePostalNomCommune(Map<String,String> tokens) {
        List<CodePostalNomCommune> list = new ArrayList<CodePostalNomCommune>();
        Iterator it = tokens.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            list.add(new CodePostalNomCommune((String)pair.getKey(), (String)pair.getValue()));
        }
        referenceRepository.persistCodePostalNomCommune(list);
    }
}
@服务
公共类ReferenceServiceImpl实现ReferenceService{
Logger Logger=LoggerFactory.getLogger(ReferenceServiceImpl.class);
@自动连线
私人参照物;
@交易的
public void loadDatabaseCodePostalNomCommunite(地图令牌){
列表=新的ArrayList();
迭代器it=tokens.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
添加(新的CodePostalNomCommune((字符串)pair.getKey(),(字符串)pair.getValue());
}
referenceposition.persistCodePostalNomCommunite(列表);
}
}
这是存储库

@Repository
public interface ReferenceRepository {
    
    void persistCodePostalNomCommune(List<CodePostalNomCommune> list);

}
and 

public class ReferenceRepositoryImpl implements ReferenceRepository {
    
    Logger logger = LoggerFactory.getLogger(ReferenceRepositoryImpl.class);

    @PersistenceContext
    private EntityManager em;

    
    @Override
    @Transactional
    public void persistCodePostalNomCommune(List<CodePostalNomCommune> list) {

        logger.info("List<CodePostalNomCommune> SIZE="+list.size());
        int i = 0;
        for (CodePostalNomCommune codePostalNomCommune : list) {
            em.persist(codePostalNomCommune);
            i++;
            if (i%20 == 0 ) {
                em.flush();
                em.clear();

            }
        } 
    }
}
@存储库
公共接口引用位置{
无效持久代码邮资公社(列表);
}
及
公共类referencepositoryimpl实现referencepository{
Logger Logger=LoggerFactory.getLogger(referencepositionimpl.class);
@持久上下文
私人实体管理者;
@凌驾
@交易的
公共无效持久代码PostalNomCommunite(列表){
logger.info(“List SIZE=“+List.SIZE());
int i=0;
for(CodePostalNomCommunite CodePostalNomCommunite:列表){
em.persist(codepastalnomucommune);
i++;
如果(i%20==0){
em.flush();
em.clear();
}
} 
}
}
但mysql数据库中没有创建任何数据


Lorem ipsum dolor sit amet,是一位杰出的献身者。我不知道。托托尔议员、艾米特议员、全国选举委员会主席、尤里西斯·塞德议员、多洛议员。乌尔特里斯元素直径。梅塞纳斯·利古拉·马萨、瓦里乌斯·阿雷乌斯、森佩尔·康格、尤伊斯莫·农、密苏里州。在培养基中,培养基必须为非发酵分子,培养基必须为非发酵直径。杜伊斯·森佩尔。两人一组,一人一组,一人一组,一人一组,一人一组。佩伦茨克康格。但在里苏斯,他是一个自由的临时法雷特拉人。从前庭到前庭。佩德中的列奥。普雷森特·布兰迪特·奥迪奥·欧·埃尼姆。佩伦茨克酒后驾车和奥古斯·布兰迪特汽水。虎口浮雕和肘部浮雕同侧前庭;阿利夸姆尼布。Mauris ac Mauris sed pellentesque发酵罐。我找到了解决办法。数据源配置不正确。在main方法类中,我添加了以下内容

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();


dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("password");
dataSource.setUrl(
  "jdbc:mysql://localhost:3306/testdb?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"); 

return dataSource;

}

有一些问题,。。。谁触发了此方法,列表中是否有条目,是否触发了
persistCodePostalNomCommunite
?您好pL4GGu33,我通过请求字符串列表触发此方法。我只是利用那个请求的机会调用了那个方法。我检查了这个方法是否被调用。日志中的所有内容和花费的时间都表明已触发39000行的持久化操作,但当我检查数据库表时,它是空的。您是否可以尝试手动打开和关闭事务。比如
em.getTransaction().begin()并在末尾
em.getTransaction().commit()您好pL4GGu33,我尝试了您建议的以下两种情况(em.getTransaction().begin();最后是em.getTransaction().commit();):删除和不删除@Transaction注释,但是它给了我一个错误java.lang.IllegalStateException:不允许在共享EntityManager上创建事务-使用Spring事务或EJB CMT代替TeadOk nevermind。。。。我现在用您的配置(包括@Transactional)尝试了一个简单的示例,它是开箱即用的。我现在有点无助,为什么我不在我们的案子里工作。你有什么特别的配置吗?