Невозможно связать два класса в Spring Boot с использованием Apache Derby (POSTMAN) - PullRequest
0 голосов
/ 06 августа 2020

Итак, я действительно застрял в этой проблеме, нет ошибки времени компиляции, но я не могу понять, где я ошибаюсь .. У меня есть два класса: один - Ребенок (идентификатор и имя как поля, идентификатор - первичный key), а второй класс - Parent (есть только поле pid), теперь GET, POST, PUT, DELETE отлично работают с дочерним классом, но когда я связываю его с родительским классом, я не получаю никакого ответа в POSTMAN. Вот классы.

ДЕТСКИЙ КЛАСС

package com.example.demo.child;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Child {
   
    @Id
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Child(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Child()
    {
        
    }

ДЕТСКИЙ КЛАСС КОНТРОЛЛЕРА

package com.example.demo.child;

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChildController {
    
    @Autowired
    private ChildService childService;
        
    @RequestMapping("/StudentList")
    public List<Child> information()
    {
        return childService.information();
    }
    
    @RequestMapping("/StudentList/{id}")
    public Optional<Child> based_on_id(@PathVariable int id)
    {
        return childService.based_on_id(id);
    }
    
    @RequestMapping(method=RequestMethod.POST,value="/StudentList")
    public void add_Student(@RequestBody Child child)
    {
        childService.add_Student(child);
    }
    
    @RequestMapping(method=RequestMethod.PUT,value="/StudentList/{id}")
    public void update_id(@RequestBody Child child)
    {
        childService.update_id(child);
    }
    
    @RequestMapping(method=RequestMethod.DELETE,value="/StudentList/{id}")
    public void delete_Student(@PathVariable int id)
    {
        childService.delete_Student(id);
    }

}

РЕБЕНОК КЛАСС ОБСЛУЖИВАНИЯ

package com.example.demo.child;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ChildService {

    
    @Autowired
    private ChildRepository childRepository;
    
    public List<Child> information() //to find all data
    {
        //return list1;
        List<Child> hello=new ArrayList<>();
        childRepository.findAll().forEach(hello::add);
        return hello;
    }

    public Optional<Child> based_on_id(int id) {
        
        return childRepository.findById(id);
    }

    public void add_Student(Child child) {
        
        childRepository.save(child);
        
    }

    public void update_id(Child child) {
         childRepository.save(child);
    }

    public void delete_Student(int id) {
    childRepository.deleteById(id);
    }
        
}

ДЕТСКИЙ РЕПОЗИТОР

package com.example.demo.child;

import org.springframework.data.repository.CrudRepository;

public interface ChildRepository extends CrudRepository<Child,Integer> {

}

РОДИТЕЛЬСКИЙ КЛАСС

package com.example.demo.standard;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import com.example.demo.child.Child;

@Entity
public class Parent {
   
    @Id
    private Integer pid;
    @OneToOne
    private Child child;
    
    
    public Integer getPid() {
        return pid;
    }
    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public Parent(Integer pid, Integer id) {
        super();
        this.pid = pid;
        this.child = new Child(id," ");
    }   
    public Child getChild() {
        return child;
    }
    public void setChild(Child child) {
        this.child = child;
    }
    public Parent()
    {
        
    }
    
}

КЛАСС РОДИТЕЛЬСКОГО КОНТРОЛЛЕРА

package com.example.demo.standard;

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.child.Child;

@RestController
public class ParentController {
    
    @Autowired
    private ParentService parentService;
        
    @RequestMapping("/StudentList/{id}/ParentList")
    public Optional<Parent> information(@PathVariable Integer id)
    {
        return  parentService.information(id);
    }
    
    @RequestMapping("/StudentList/{id}/ParentList/{pid}")
    public Optional<Parent>  based_on_id(@PathVariable Integer pid)
    {
        return parentService.based_on_id(pid);
    }
    
    @RequestMapping(method=RequestMethod.POST,value="/StudentList/{id}/ParentList")
    public void add_Parent(@RequestBody Parent parent,@PathVariable Integer id)
    {
        parent.setChild(new Child(id,""));
        parentService.add_Parent(parent);
    }
    
    @RequestMapping(method=RequestMethod.PUT,value="/StudentList/{id}/ParentList/{pid}")
    public void update_id(@RequestBody Parent parent,@PathVariable Integer id,@PathVariable Integer pid)
    {   
        parent.setChild(new Child(id,""));
        parentService.update_Parent(parent);
    }
    
    @RequestMapping(method=RequestMethod.DELETE,value="/StudentList/{id}/ParentList/{pid}")
    public void delete_Parent(@PathVariable int id)
    {
        parentService.delete_Parent(id);
    }

}

КЛАСС РОДИТЕЛЬСКОГО ОБСЛУЖИВАНИЯ

package com.example.demo.standard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ParentService {

    
    @Autowired
    private ParentRepo parentRepository;
    
     public Optional<Parent> information(Integer id) //to find all data
    {
        return parentRepository.findById(id);
    } 

    public  Optional<Parent> based_on_id(Integer id) {
        
        return parentRepository.findById(id);
    }

    public void add_Parent(Parent parent) {
        
        parentRepository.save(parent);
        
    }

    public void update_Parent(Parent parent) {
         parentRepository.save(parent);
    }

    public void delete_Parent(int id) {
    parentRepository.deleteById(id);
    }
        
}

РОДИТЕЛЬСКОЕ ХРАНИЛИЩЕ

package com.example.demo.standard;

import org.springframework.data.repository.CrudRepository;

public interface ParentRepo extends CrudRepository<Parent,Integer> {

    
}
...