Java 未将计划数据保存到JPA
我觉得这代码很有趣。但数据未保存,但hello正在打印,请指导我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() {
@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();}