2017-05-07 181 views
0
@RestController 
public class TopicController { 

    @Autowired 
    private TopicService topicService; 

    @RequestMapping(value="/topics", method= RequestMethod.GET) 
    public List<Topic> getAllTopics(){ 
     return topicService.getAllTopics(); 
    } 

    @RequestMapping(value="/topics/{id}", method= RequestMethod.GET) 
    public Topic getTopic(@PathVariable String id){ 
     return topicService.getTopic(id); 
    } 

    @RequestMapping(value="/topics", method= RequestMethod.POST) 
    public void addTopic(@RequestBody Topic topic){ 
     topicService.addTopic(topic); 
    } 

    @RequestMapping(value="/topics/{id}", method= RequestMethod.PUT) 
    public void updateTopic(@RequestBody Topic topic, @PathVariable String id){ 
     topicService.updateTopic(id, topic); 
    } 

    @RequestMapping(value="/topics/{id}", method= RequestMethod.DELETE) 
    public void deleteTopic(@PathVariable String id){ 
     topicService.deleteTopic(id); 
    } 
} 

Controller類的列表POST項目春季啓動

@Service 
public class TopicService { 

    @Autowired 
    private TopicRepository topicRepo; 

    public List<Topic> getAllTopics(){ 
     return (List<Topic>)topicRepo.findAll(); 

    } 

    public Topic getTopic(String id){ 
     return topicRepo.findOne(id); 
    } 

    public void addTopic(Topic topic){ 
     //topics.add(topic); 

     topicRepo.save(topic); 
    } 

    public void updateTopic(String id, Topic topic) { 
     topicRepo.save(topic); 
    } 

    public void deleteTopic(String id) { 
     //topics.removeIf(t -> t.getId().equals(id)); 

     //topics.removeIf((Topic t) -> t.getId().equals(id)); 

     topicRepo.delete(id); 

    } 

} 

服務類

@Repository 
public interface TopicRepository extends CrudRepository<Topic, String>{ 

    //List<Course> findByTopic_Id(String topicid); 

} 

倉儲類

@Entity 
public class Topic { 

    @Id 
    @Column(name="TOPIC_ID") 
    private String id; 
    @Column(name="NAME") 
    private String name; 
    @Column(name="DESCRIPTION") 
    private String description; 

    @OneToMany(mappedBy="topic", fetch = FetchType.EAGER) 
    @JsonManagedReference 
    private List<Course> course = new ArrayList<Course>(); 

    //no - argument constructor. Needed for hibernate 
    public Topic(){}; 

    public Topic(String id, String name, String description, List<Course> course){ 
     super(); 
     this.id = id; 
     this.name = name; 
     this.description = description; 
     this.course = course; 
    } 

    public Topic(String id, String name, String description){ 
     super(); 
     this.id = id; 
     this.name = name; 
     this.description = description; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public List<Course> getCourse() { 
     return course; 
    } 

    public void setCourse(List<Course> course) { 
     this.course = course; 
    } 
} 

主題類

@Entity 
public class Course{ 

    @Id 
    @Column(name="COURSE_ID") 
    private String id; 
    private String name; 
    private String description; 

    //There could be many courses related to 1 topic 
    @ManyToOne 
    @JoinColumn(name = "TOPIC_ID") 
    @JsonBackReference 
    private Topic topic; 

    public Course(){}; 


    public Course(String id, String name, String description){ 

     super(); 
     this.id = id; 
     this.name = name; 
     this.description = description; 

    } 

    public Topic getTopic() { 
     return topic; 
    } 

    public void setTopic(Topic topic) { 
     this.topic = topic; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

} 

課程班

我試圖用郵差發佈一個包含很多課程到我的SQL數據庫中的主題類。

在郵差,我沒有使用JSON這樣

{ 
    "id": "700", 
    "name": "How to countt", 
    "description": "Counting numbersssss", 
    "course": [ 
     { 
     "id": "1", 
     "name": "php", 
     "description": "gooddddyyyy stuff" 
     }, 
     { 
     "id": "2", 
     "name": "phpp", 
     "description": "gooddddyyyy stuffp" 
     } 
    ] 
} 

然而,POST,當我做了相應的得到所有的話題,我的反應是

{ 
    "id": "700", 
    "name": "How to countt", 
    "description": "Counting numbersssss", 
    "course": [] 
} 

它不是拿起課程我發佈的。一個主題可以有很多課程。我該如何解決?謝謝

回答

0

您從未設置雙向關聯的擁有方:Course.topic

而且沒有在Topic.courses上設置級聯。

因此,不僅保存一個主題不會保存它的課程,但即使這樣做,這些課程也不屬於他們的主題。

+0

這是否意味着我必須像Topic.setcourse(課程)那樣將課程對象傳入主題? – Desmond

+0

編號A主題沒有一門課,它有很多。這個課程集合由傑克遜填充。相反是不正確的。課程沒有提及他們的主題。所以你需要設置每門課程的主題:'course.setTopic(topic)'。 –