2

I have a problem with saving entity with references to database using Spring MVC and AngularJS.

My entities:

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;

    ...
}

How can I save new Article but at the same time add new entity to Comment table? How can two updates be performed in one transaction? How sholud I connect request from angular with controller, and how sholud they look?

Angular:

$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?
}

Thank You!

user3096746
  • 61
  • 1
  • 12

0 Answers0