1

tengo dos tablas relacionadas , teacher y course donde course tiene como fk el id de teacher , esta relacionado de una a muchos pero , al hacer un insert no me esta ingresando el id del teacher en mi tabla course , la entidad teacher esta onetomany y en la entidad course esta maytoOne , en otras palabras un teacher puede tener varios course pero un course puede tener un solo profesor¿Cuál seria el error?

esta es mi entidad teacher

package com.ebim.teachers.models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name="teacher")
public class Teacher{
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Integer tea_id;
    public String tea_nam;
    public String tea_ava;
    @OneToMany(mappedBy = "teacher",cascade = CascadeType.ALL)
    public List<Course> course = new ArrayList<>();
    public Teacher() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    public Teacher(String tea_nam, String tea_ava, List<Course> course) {
        super();
        this.tea_nam = tea_nam;
        this.tea_ava = tea_ava;
        this.course = course;
    }


    public Integer getTea_id() {
        return tea_id;
    }
    public void setTea_id(Integer tea_id) {
        this.tea_id = tea_id;
    }
    public String getTea_nam() {
        return tea_nam;
    }
    public void setTea_nam(String tea_nam) {
        this.tea_nam = tea_nam;
    }
    public String getTea_ava() {
        return tea_ava;
    }
    public void setTea_ava(String tea_ava) {
        this.tea_ava = tea_ava;
    }
    public List<Course> getCourse() {
        return course;
    }
    
    public void setCourse(List<Course> course) {
        this.course = course;
    }
}

esta es mi entidad course

package com.ebim.teachers.models;



import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name="course")
public class Course{
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Integer cou_id;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="tea_id")
    public Teacher teacher;
    public String cou_nam;
    public String cou_the;
    public String cou_pro;
    
    public Course() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    public Course(Teacher teacher, String cou_nam, String cou_the, String cou_pro) {
        super();
        this.teacher = teacher;
        this.cou_nam = cou_nam;
        this.cou_the = cou_the;
        this.cou_pro = cou_pro;
    }


    public Integer getCou_id() {
        return cou_id;
    }
    public void setCou_id(Integer cou_id) {
        this.cou_id = cou_id;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public String getCou_nam() {
        return cou_nam;
    }
    public void setCou_nam(String cou_nam) {
        this.cou_nam = cou_nam;
    }
    public String getCou_the() {
        return cou_the;
    }
    public void setCou_the(String cou_the) {
        this.cou_the = cou_the;
    }
    public String getCou_pro() {
        return cou_pro;
    }
    public void setCou_pro(String cou_pro) {
        this.cou_pro = cou_pro;
    }

}

este es mi controller

package com.ebim.teachers.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ebim.teachers.models.Teacher;
import com.ebim.teachers.servicesImpl.TeacherSeviceImpl;

@RestController
@RequestMapping("/api/profesores")
public class TeacherController {
    
    @Autowired
    private TeacherSeviceImpl teacherServiceImpl;
    
    @PostMapping(value="/teacherCourse",consumes = "application/json",produces="application/json")
    public Teacher nuevoProfe(@RequestBody Teacher teacher) {
        
        return teacherServiceImpl.newTeacher(teacher);
    }
}

estas son mi tablas en mi bd

create table teacher(
tea_id int primary key identity(1,1),
tea_nam varchar(100),
tea_ava varchar(250),
);

create table course(
cou_id int primary key identity(1,1),
tea_id int,
cou_nam varchar(100),
cou_the varchar(100),
cou_pro varchar(100),
);

el JSON que envio

{
    "tea_nam":"juanita",
    "tea_ava":"SGzdgvDsdgsfxdhx",
    "course":[
        {
            "cou_nam":"rm",
            "cou_the":"rrhhh",
            "cou_pro":"concurso"
        },
        {
            "cou_nam":"rm",
            "cou_the":"personal social",
            "cou_pro":"concurso"
        }
    ]
}

resultado en postman

{
    "tea_id": 2,
    "tea_nam": "juanita",
    "tea_ava": "SGzdgvDsdgsfxdhx",
    "course": [
        {
            "cou_id": 1,
            "teacher": null,
            "cou_nam": "rm",
            "cou_the": "rrhhh",
            "cou_pro": "concurso"
        },
        {
            "cou_id": 2,
            "teacher": null,
            "cou_nam": "rm",
            "cou_the": "personal social",
            "cou_pro": "concurso"
        }
    ]
}

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.