2017-05-07 2 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-KlassePOST eine Liste der Elemente Federverschluß

@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); 

    } 

} 

Service Class

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

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

} 

Repository Klasse

@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; 
    } 
} 

Thema Klasse

@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; 
    } 

} 

Course Klasse

Ich versuche Postman zu verwenden, um ein Thema Klasse zu schreiben, die viele Kurse in meine SQL-Datenbank enthält.

In Postman, ich die POST tat JSON verwenden ähnliche

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

Allerdings, wenn ich die entsprechende alle Themen erhalten tun, meine Antwort war

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

Es ist nicht die Kurse Abholung das habe ich gepostet. Ein Thema kann viele Kurse haben. Wie behebe ich das? Danke

Antwort

0

Sie setzen nie die besitzende Seite der bidirektionalen Zuordnung: Course.topic.

Und es gibt keine Kaskade auf Topic.courses gesetzt.

Also nicht nur ein Thema zu speichern wird nicht seine Kurse speichern, aber selbst wenn dies der Fall wäre, würden die Kurse nicht zu ihrem Thema gehören.

+0

Bedeutet das, dass ich wie Topic.setcourse (Kurs) tun muss, um das Kursobjekt in das Thema zu übergeben? – Desmond

+0

Nein. Ein Thema hat keinen Kurs, es hat viele. Und diese Sammlung von Kursen wird von Jackson bevölkert. Das Gegenteil ist nicht wahr. Die Kurse haben keinen Bezug zu ihrem Thema. Sie müssen also das Thema für jeden Kurs festlegen: 'course.setTopic (topic)'. –

Verwandte Themen