保存实体Spring MVC Angular时更新多个表

时间:2016-03-16 12:02:32

标签: angularjs spring rest entity multiple-tables

我在使用对数据库的引用保存实体时遇到问题 Spring MVC和AngularJS。

我的实体:

Entity
@Table(name = "comment")
public class Comment {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "creation_date", nullable = false)
    private Date creationDate;

    @Column(name = "comment", nullable = false)
    private String comment;

    @ManyToOne
    @JoinColumn(name = "article", nullable = false)
    private Article article;

    @ManyToOne
    @JoinColumn(name = "user", nullable = false)
    private User user;
}


@Entity
@Table(name = "article")
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name="user", nullable=false)
    private User user;

    ...
}

如何保存新文章但同时在评论表中添加新实体? 如何在一个事务中执行两个更新? 我如何通过控制器将角度请求与角度连接起来,以及它们看起来有多么糟糕?

角:

$http({
    url : ..../new,
    method : POST,
    data : ?
})

@RestController

@RequestMapping(value = "/new", method = RequestMethod.POST)
public Article addArticle(@RequestBody Article article, ?) {
    article.setId(null);        
    return articleService.save(article);
    //but how to save comment?
}

谢谢!

0 个答案:

没有答案