Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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
Java 未将计划数据保存到JPA_Java_Spring_Hibernate_Spring Data Jpa - Fatal编程技术网

Java 未将计划数据保存到JPA

Java 未将计划数据保存到JPA,java,spring,hibernate,spring-data-jpa,Java,Spring,Hibernate,Spring Data Jpa,我觉得这代码很有趣。但数据未保存,但hello正在打印,请指导我 @Service public class TestService { @Autowired TestRepository testRepo; final static Logger log = Logger.getLogger(TestService.class); @PostConstruct public void init() { new Thread() {

我觉得这代码很有趣。但数据未保存,但hello正在打印,请指导我

@Service
public class TestService {

    @Autowired
    TestRepository testRepo;

    final static Logger log = Logger.getLogger(TestService.class);

    @PostConstruct
    public void init() {
        new Thread() {
            public void run() {
                try {
                    while (true) {
                        process();
                        Thread.sleep(10000);
                    }

                } catch (Exception exception) {
                    log.info("Exception while starting schedular :" + exception);
                }

            }
        }.start();
    }

    @Transactional
    public void process() {
        Test t=new Test(); 
        t.id=null;
        t.name="Lokesh";
        testRepo.save(t);
       log.info("hello");
    }
}

问题是Lokesh并没有为表测试保存…

Spring使用代理应用AOP。这意味着AOP只会处理对对象的方法调用。您正在执行内部方法调用,因此基本上您的
@Transactional
被忽略。如果没有事务,将无法保存任何内容。还有,为什么在init方法中会出现丑陋的黑客行为,这会让事情变得更糟,因为它甚至不能保证代理已经创建了bean。基本上,您的代码在多个级别上都是错误的。请编写TestRepository.class.TestRepository。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。公共接口TestRepository扩展了JpaRepository{Test findById(长id);List findAll();}