Many-to-many or many-to-many spring-data-jpa

To-many, many-to

Country Class

@Entity
@Table(name = "Country")
public class Country {
    @Id
    // Sequence the Identity 
    @GeneratedValue (at Strategy = GenerationType.IDENTITY)
     Private Integer countryId;
     Private String Country;
     // the mappedBy: specify who maintain relationships (set is the attribute name associated objects) 
    @OneToMany (Cascade = CascadeType.ALL, = the mappedBy " Country " )
     // @JoinColumn (name = "countryId") 
    Private List <City> = the citys new new the ArrayList <> (); // association properties

City class

@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 layer

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

CityDao layer

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

CountryController

@Controller
public class CountryController {

    @Autowired
    CountryDao dao;

    @Autowired
    CityDao dao1;

    @Autowired
    StudentDao dao2;

    @Autowired
    TeacherDao dao3;

    // cascade increase 
    @ RequestMapping ( " / OneToMany " )
     public String OneToMany () {
        Country c1=new Country();
        c1.setCountry ( " Long live China CHINANO. " );
        City ct1=new City();
        ct1.setCityname ( " Hong Kong Chinese " );
        City ct2=new City();
        ct2.setCityname ( " China Taiwan " );

        // maintenance-many relationship between the state and the city 
        c1.getCitys () add (ct1). ;
        c1.getCitys().add(ct2);

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

    // related inquiries 
    @ RequestMapping ( " / getCountry " )
    @ResponseBody
    public Object get(){
        return dao.findAll();
    }

    // cascading deletes 
    @ RequestMapping ( " / deleteCountry " )
     public String the Delete () {
         // retrieve state entities 
        Country = dao.getOne One ( 1 );
        dao.delete(one);
        return "success";
    }

    // from the city to the country associated with the query 
    @ RequestMapping ( " / getCity " )
    @ResponseBody
    public Object getCity(){
        return dao1.findAll();
    }

    // add students and teachers 
    @ RequestMapping ( " / saveStudent " )
     public String the Save () {
        Student1 Student = new new Student ( " Elijah East Brother " );
        Student student2=new Student("玉姐");
        Student student3=new Student("雄哥");

        T1 Teacher = new new Teacher ( " the wind in the mountains " );
        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(){
        T1 Teacher = new new Teacher ( " handsome ridiculously " );
        List<Student> all = dao2.findAll();
        t1.getStudents().addAll(all);
        dao3.save(t1);
        return "success";
    }

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

Many to many

Teacher

@Entity
@Table(name="teacherinfo")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer teacherid;
    private String teachername;
    @ManyToMany (Cascade = CascadeType.ALL)
     // the mappedBy property absolutely can not exist when using annotations @JoinTable and @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;

    // add students and teachers 
    @ RequestMapping ( " / addstu " )
    @ResponseBody
    public String addstu(){
        Student1 Student = new new Student ( "Lily " );
        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";
    }

    // many-added teacher 
    @ RequestMapping ( " / addDom " )
    @ResponseBody
    public String addDom(){
        Teacher Teacher = new new Teacher ( " Li " );
        List<Student> all = studentDao.findAll();
        teacher.getStudents().addAll(all);
        teacherDao.save(teacher);
        return "SUCCESS";
    }

    // many association query (caution !! infinite loop !!) 
    @ RequestMapping ( " / getDom " )
    @ResponseBody
    public Object getDom(){
        return teacherDao.getOne(1);
    }
}

 

Guess you like

Origin www.cnblogs.com/dabrk/p/12002926.html