一對多,多對一

多端:

package com.livenaked.hub.odm.entity.user;


import org.hibernate.envers.Audited;
import javax.persistence.*;

@Entity
@Table(name = "t_student")
public class Student {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    //若是隻是加上@ManyToOne  @JoinColumn(name = "tid"),會產生外鍵
    //加上foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),jpa不會建立外鍵約束
    //一端默認就是當即加載此處fetch = FetchType.EAGER能夠省略
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "teacher_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    private Teacher teacher;

    public Student() {

    }

    public Student(String name) {
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Product [id=" + id + ", name=" + name + "]";
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

}

 

 

一端:

package com.livenaked.hub.odm.entity.user;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "t_teacher")
@SQLDelete(sql = "update nh_odm_t_teacher set isDeleted=1, updateTime=now() where id = ? and version = ?")
@Where(clause = "isDeleted=0")
public class Teacher {


    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private boolean isDeleted;

    @Version
    private long version;

    private Date updateTime;

    private Date createTime;

    //teacher_id:外鍵名稱
    //若是隻是加上@OneToMany  @JoinColumn(name = "teacher_id") 會產生外鍵
    //cascade = CascadeType.PERSIST 級聯保存
    //多的一端默認是t FetchType.LAZY
    @OneToMany(cascade = {CascadeType.PERSIST}, fetch = FetchType.LAZY)
    @JoinColumn(name = "teacher_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    @org.hibernate.annotations.ForeignKey(name = "none")
    private Set<Student> students = new HashSet<>();


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDeleted() {
        return isDeleted;
    }

    public void setDeleted(boolean deleted) {
        isDeleted = deleted;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void setStudents(Set<Student> students) {
        this.students = students;
    }

    @Override
    public String toString() {
        return "ProductType [id=" + id + ", name=" + name + "]";
    }
}

 

 

ManyToOneTest:

package com.livenaked.hub.odm.service;


import com.livenaked.hub.odm.dao.StudentRepository;
import com.livenaked.hub.odm.entity.user.Student;
import com.livenaked.hub.odm.entity.user.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class ManyToOneTest {

    @Autowired
    private StudentRepository repository;

    @Test
    public void save(){
        Student s = new Student();
        s.setName("s_audit1");

        Teacher t = new Teacher();
        t.setId(1L);

        s.setTeacher(t);

        repository.save(s);
    }

    @Test
    public void query() {
        Student s = repository.findOne(2L);
        Student student = repository.findOne(2L);
        Teacher teacher = student.getTeacher();
        System.out.println(teacher.getName());
    }





}

 

OneToManyTest:

package com.livenaked.hub.odm.service;


import com.livenaked.hub.odm.dao.StudentRepository;
import com.livenaked.hub.odm.dao.TeacherRepository;
import com.livenaked.hub.odm.entity.user.Student;
import com.livenaked.hub.odm.entity.user.Teacher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
public class OneToManyTest {

    @Autowired
    private TeacherRepository teacherRepository;

    @Autowired
    private StudentRepository studentRepository;

    //這個測試會產生10個insert,10個update
    @Test
    public void save1() {
        Teacher teacher = new Teacher();
        teacher.setName("werwer");
        teacher.setCreateTime(new Date());
        teacher.setUpdateTime(new Date());

        Set<Student> students = new HashSet<>();
        for (long i = 0; i < 10; i++) {
            Student stu = new Student();
            stu.setName("stu" + i);
            stu.setTeacher(teacher);
            students.add(stu);
        }

        teacherRepository.save(teacher);
        studentRepository.save(students);
    }

    @Test
    public void deleteTeacher() {
        Teacher teacher = teacherRepository.findOne(1L);
        teacher.setVersion(0L);
        teacherRepository.delete(teacher);
    }


    @Test
    public void save2() {
        Teacher teacher = new Teacher();
        teacher.setName("werwer");

        Student s1 = new Student();
        s1.setName("s1");
        s1.setTeacher(teacher);

        Student s2 = new Student();
        s2.setName("s2");
        s2.setTeacher(teacher);

        teacherRepository.save(teacher);
        studentRepository.save(s1);
        studentRepository.save(s2);
    }

    @Test
    public void update() {
        Teacher t = teacherRepository.findOne(1L);
        t.setVersion(t.getVersion() + 1);
        t.setName("ewwwwwwwwwwwddddwdwwww");
        teacherRepository.save(t);
    }

    @Test
    public void query() {
        Teacher teacher = teacherRepository.findOne(1L);
        Set<Student> students = teacher.getStudents();
        for (Student student : students) {
            System.out.println(student.getId() + " : " + student.getName());
        }
    }

}
相關文章
相關標籤/搜索