使用Spring JPA的更改未持久保存在Spring Boot应用程序的数据库中

时间:2018-10-18 22:41:53

标签: spring-boot spring-data-jpa

我有一个Spring Boot应用程序,需要在供稿列表中添加帖子。帖子由用户撰写,由内容和几个附件组成。该数据库有3个表:发布,附件和用户。

应用程序的主要类别是:

@SpringBootApplication
public class SocialMediaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SocialMediaApplication.class, args);
    }
}

实体如下:

Post.java

@Entity
public class Post implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false)
    private String content;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @Column(nullable = false)
    private Timestamp createdAt;

    @Column
    private String location;

    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
    private List<Attachment> attachmentList;

    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
    private List<Rating> ratingList;

    public Post() {
    }

    public Post(String content, User user, Timestamp createdAt, String location, List<Attachment> attachmentList, List<Rating> ratingList) {
        super();
        this.content = content;
        this.user = user;
        this.createdAt = createdAt;
        this.location = location;
        this.attachmentList = attachmentList;
        this.ratingList = ratingList;
    }

    // ...
}

Attachment.java

@Entity
public class Attachment implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Lob
    @Column(length = 100_000, nullable = false)
    private byte[] content;

    @ManyToOne
    @JoinColumn(name = "post_id")
    private Post post;

    public Attachment() {
    }

    public Attachment(byte[] content, Post post) {
        super();
        this.content = content;
        this.post = post;
    }

    // ...
}

User.java

@Entity
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String firstName;

    @Column(nullable = false)
    private String lastName;

    @Column(nullable = false)
    private Date dateOfBirth;

    @Column(nullable = false)
    private String credential;

    @Column(nullable = false)
    private String password;

    @Column
    private String location;

    @Lob
    @Column(length = 100_000)
    private byte[] photo;

    @Column
    private String motto;

    public User() {
    }

    public User(String firstName, String lastName, Date dateOfBirth, String credential, String password,
            String location, byte[] photo, String motto) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.dateOfBirth = dateOfBirth;
        this.credential = credential;
        this.password = password;
        this.location = location;
        this.photo = photo;
        this.motto = motto;
    }

    // ...
}

所有存储库都扩展了CrudRepository并以@Transactional进行注释:

PostRepository.java

@Transactional
public interface PostRepository extends CrudRepository<Post, Long> {

}

AttachmentRepository.java

@Transactional
public interface AttachmentRepository extends CrudRepository<Attachment, Long> {

}

UserRepository.java

@Transactional
public interface UserRepository extends CrudRepository<User, Long> {

}

应在提要中添加帖子的控制器如下:

@Controller
@RequestMapping("/post")
public class PostController {

    @Autowired
    PostRepository postRepository;

    @GetMapping("/add")
    public String greetingForm(Model model) {
        model.addAttribute("post", new Post());
        return "addPost";
    }

    @PostMapping("/add")
    public String addPost(@ModelAttribute Post post, @RequestParam("attachment") MultipartFile uploadingFile) throws IOException {
        User user = new User();
        user.setId(1L);
        post.setUser(user);
        post.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
        List<Attachment> attachmentList = new ArrayList<>();
        Attachment attachment = new Attachment();
        attachment.setContent(uploadingFile.getBytes());
        attachment.setPost(post);
        attachmentList.add(attachment);
        post.setAttachmentList(attachmentList);
        List<Rating> ratingList = new ArrayList<>();
        post.setRatingList(ratingList);
        postRepository.save(post);
        return "allPosts";
    }

}

addPost.html 页面具有以下内容:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Add Post</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Add Post</h1>
    <form action="#" th:action="@{/post/add}" th:object="${post}" method="post" enctype="multipart/form-data">
    	<table border="0">
    		<tr>
    			<td>Content</td>
    			<td><textarea id="content" th:field="*{content}" rows="5" cols="50"></textarea></td>
    		</tr>
    		<tr>
    			<td>Location</td>
    			<td><input type="text" id="location" th:field="*{location}"/></td>
    		</tr>
    		<tr>
    			<td>Attachment</td>
    			<td><input type="file" id="attachment" name="attachment"/></td>
    		</tr>
    		<tr>
    			<td colspan="2">
    				<input type="submit" value="Submit" />
    				<input type="reset" value="Reset" />
    			</td>
    		</tr>
        </table>
    </form>
</body>
</html>

application.properties 文件具有以下内容:

spring.datasource.url=jdbc:mysql://localhost:3306/****?useSSL=false
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.show-sql=true

spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

但是,当我按下“提交”按钮时,尽管查询仍在控制台中显示,但没有任何内容持久保存在数据库中:

Hibernate: insert into post (content, created_at, location, user_id) values (?, ?, ?, ?)
Hibernate: insert into attachment (content, post_id) values (?, ?)

可能是什么原因?

0 个答案:

没有答案