java Springboot-JPA不保存数据库中的数据

bzzcjhmw  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(171)

Springboot-JPA在wildfly上部署时不将数据插入数据库,但在STS中执行相同代码时将数据插入数据库。

1 www.example.com

spring.datasource.hikari.auto-commit=false
spring.servlet.multipart.enabled=true
logging.level.org.springframework.web=DEBUG
spring.jpa.hibernate.ddl-auto = update
spring.main.allow-circular-references:true
spring.main.web-environment=false

2控制器

@RestController
@RequestMapping("/api")
@EnableWebMvc
public class BlogController {
    @Autowired
    BlogService blogService;
    // create blog entry 
    @RequestMapping(value = "/writeblog", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody Blog createBlog(@RequestParam("file") MultipartFile file, @ModelAttribute Blog blogContent)
            throws IOException {
        return blogService.saveBlogContent(file, blogContent);
    }
}

3服务

@Service
@Transactional
public class BlogService {
    @Lazy
    @Autowired
    private BlogRepository blogRepository;

    public Blog saveBlogContent(MultipartFile file, Blog blogContent) throws IOException {
        String blogImgURl = ServletUriComponentsBuilder.fromCurrentContextPath().path("/api/blogimage/").toUriString();
        
        long millis = System.currentTimeMillis();
        java.sql.Date date = new java.sql.Date(millis);
        blogContent.setImage(file.getBytes());
        
        blogContent.setPublishDate(date);
        blogContent.setBlogImageUrl(blogImgURl);
        Blog blog = blogRepository.saveAndFlush(blogContent);
        return blog;
    }
}

4存储库

@Repository
public interface BlogRepository extends JpaRepository<Blog, Long>{

    Blog findByBlogId(Long blogId);
}

5我正在获取带有自动生成ID的对象,正如我在模型中使用的下面代码:(@GeneratedValue(strategy = GenerationType.IDENTITY)private long blogId;退货:

{ "blogId": 10, 
"employeeId": 202, 
"blogTitle": "blog Title TEST-55", 
"blogContent": "blog CONTENT TESTING-555", 
"image": "" 
}

当我从STS IDE执行代码时,它会将值保存在数据库中,但当我在wildfly服务器上部署war文件时,它不会将其保存在database中。

请告诉我为什么相同的代码表现不同,或者我错过了什么?

7ivaypg9

7ivaypg91#

请添加Main class @EnableJpaRepositories

相关问题