spring-data-jpa一对多多对一多对多关联

Posted 慕容子月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-data-jpa一对多多对一多对多关联相关的知识,希望对你有一定的参考价值。

一对多、多对一

Country类

@Entity
@Table(name = "Country")
public class Country {
    @Id
    //sequence identity
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer countryid;
    private String country;
    //mappedBy:指定由谁维护关联关系(设置的是关联对象的属性名)
    @OneToMany(cascade = CascadeType.ALL,mappedBy = "country")
    //@JoinColumn(name="countryid")
    private List<City> citys=new ArrayList<>(); //关联属性

City类

@Entity
@Table(name = "City")
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer cityid;
    private String cityname;
    @ManyToOne(cascade = CascadeType.ALL)
    private Country country;

CountryDao层

public interface CountryDao extends JpaRepository<Country,Integer>{
}

CityDao层

public interface CityDao extends JpaRepository<City,Integer>{
}

CountryController

@Controller
public class CountryController {

    @Autowired
    CountryDao dao;

    @Autowired
    CityDao dao1;

    @Autowired
    StudentDao dao2;

    @Autowired
    TeacherDao dao3;

    //级联增加
    @RequestMapping("/onetomany")
    public String onetomany(){
        Country c1=new Country();
        c1.setCountry("中国万岁 ChinaNo.1");
        City ct1=new City();
        ct1.setCityname("中国香港");
        City ct2=new City();
        ct2.setCityname("中国台湾");

        //维护国家与城市的一对多关系
        c1.getCitys().add(ct1);
        c1.getCitys().add(ct2);

        dao.save(c1);
        return "success";
    }

    //关联查询
    @RequestMapping("/getcountry")
    @ResponseBody
    public Object get(){
        return dao.findAll();
    }

    //级联删除
    @RequestMapping("/deleteCountry")
    public String delete(){
        //检索国家实体
        Country one = dao.getOne(1);
        dao.delete(one);
        return "success";
    }

    //由城市到国家的关联查询
    @RequestMapping("/getCity")
    @ResponseBody
    public Object getCity(){
        return dao1.findAll();
    }

    //添加学生和老师
    @RequestMapping("/saveStudent")
    public String save(){
        Student student1=new Student("利亚东哥");
        Student student2=new Student("玉姐");
        Student student3=new Student("雄哥");

        Teacher t1=new Teacher("山间的风");
        student1.getTeachers().add(t1);
        student2.getTeachers().add(t1);
        student3.getTeachers().add(t1);
        dao2.saveAll(Arrays.asList(student1,student2,student3));

        return "success";

    }

    @RequestMapping("/saveTeacher")
    public String saveTeacher(){
        Teacher t1=new Teacher("帅的离谱");
        List<Student> all = dao2.findAll();
        t1.getStudents().addAll(all);
        dao3.save(t1);
        return "success";
    }

    @RequestMapping("/getTeacher")
    @ResponseBody
    public Object getTeacher(){
        return dao3.getOne(1);
    }
}

多对多

Teacher

@Entity
@Table(name="teacherinfo")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer teacherid;
    private String teachername;
    @ManyToMany(cascade = CascadeType.ALL)
    //mappedby属性绝对不能再存在@JoinTable和@JoinColumn注解时使用
    @JoinTable(name="student_teacher",joinColumns =@JoinColumn(name="teacherid") ,
            inverseJoinColumns =@JoinColumn(name="studentid") )
    private List<Student> students=new ArrayList<>();

Student

@Entity
@Table(name = "studentinfo")
public class Student {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
    @Column(name = "stu_name")

TeacherDao

public interface TeacherDao extends JpaRepository<Teacher,Integer> {
}

StudentDao

public interface StudentDao extends JpaRepository<Student,Integer>{
}

StudentController

@Controller
public class StudentController{

    @Autowired
    StudentDao studentDao;

    @Autowired
    TeacherDao teacherDao;

    //添加学生和老师
    @RequestMapping("/addstu")
    @ResponseBody
    public String addstu(){
        Student student1=new Student("丽丽");
        Student student2=new Student("明明");
        Student student3=new Student("安安");

        Teacher teacher1=new Teacher("筱丽");
        student1.getTeachers().add(teacher1);
        student2.getTeachers().add(teacher1);
        student3.getTeachers().add(teacher1);

        studentDao.saveAll(Arrays.asList(student1,student2,student3));
        return "SUCCESS";
    }

    //多对多添加老师
    @RequestMapping("/addDom")
    @ResponseBody
    public String addDom(){
        Teacher teacher=new Teacher("李老师");
        List<Student> all = studentDao.findAll();
        teacher.getStudents().addAll(all);
        teacherDao.save(teacher);
        return "SUCCESS";
    }

    //多对多关联查询(慎用!!死循环!!)
    @RequestMapping("/getDom")
    @ResponseBody
    public Object getDom(){
        return teacherDao.getOne(1);
    }
}

 

以上是关于spring-data-jpa一对多多对一多对多关联的主要内容,如果未能解决你的问题,请参考以下文章

数据库 - 一对一对多多对多

Mybatis 一对一对多多对多

3Hibernate一对一对多多对多对应关系

七一对一对多多对多

mybatis映射 一对一一对多多对多高级映射

Mybatis一对一对多多对多