zoukankan      html  css  js  c++  java
  • 学生-课程-成绩表设计

    学生:

    @Entity
    public class Student {
        private int id;
        private String name;
        private Set<Course> courses = new HashSet<Course>();
        @Id
        @GeneratedValue
        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;
        }
    
        @ManyToMany
        @JoinTable(name="score",//与Score中设置一致
                joinColumns=@JoinColumn(name="student_id"),
                inverseJoinColumns=@JoinColumn(name="course_id")
                )
        public Set<Course> getCourses() {
            return courses;
        }
        public void setCourses(Set<Course> courses) {
            this.courses = courses;
        }
    }

    课程:

    @Entity
    public class Course {
        private int id;
        private String name;
        @Id
        @GeneratedValue
        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;
        }
    }

    成绩:

    @Entity
    @Table(name="score")
    public class Score {
        private int id;
        private int score;
        private Student student;
        private Course course;
        
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        @ManyToOne
        @JoinColumn(name="student_id")
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        }
        @ManyToOne
        @JoinColumn(name="course_id")
        public Course getCourse() {
            return course;
        }
        public void setCourse(Course course) {
            this.course = course;
        }
        public int getScore() {
            return score;
        }    
        public void setScore(int score) {
            this.score = score;
        }    
    }

    junit:

    public class TestTree {
        private static SessionFactory sf;
        @Before
        public void beforeTest(){
            sf = new Configuration().configure().buildSessionFactory();
        }
        @After
        public void afterTest(){
            sf.close();
        }
        
        @Test
        public void testSave(){
            Student s = new Student();
            s.setName("张三");
            Course yuwen = new Course();
            yuwen.setName("语文");
            Score score = new Score();
            score.setCourse(yuwen);
            score.setStudent(s);
            score.setScore(90);
            
            Session sess = sf.getCurrentSession();
            sess.beginTransaction();
            
            sess.save(s);
            sess.save(yuwen);
            sess.save(score);
            sess.getTransaction().commit();
    
        }
        
        @Test
        public void testLoad(){
            Session sess = sf.getCurrentSession();
            sess.beginTransaction();
            Student s = (Student)sess.load(Student.class, 2);
            for(Course c : s.getCourses()){
                System.out.println(c.getName());
            }
            sess.getTransaction().commit();
    
        }
    }
  • 相关阅读:
    ASP.NET State Service
    C# winform 打印当前窗体
    js计算时间之差
    转 Javascript中event.keyCode键码值表收藏
    从客户端中检测到有潜在危险的 Request.Form
    从VS2005项目转换为VS2008项目 转载
    C# Windows DataGridView 判断CheckBox 选取的方法
    在Visual Studio 2005中调试SQL Server 2005的存储过程
    ASP.NET Ajax 中出现的 sys 未定义(sys undefined)解决方法总结
    KubeMeet 直播 | 现场直击大规模集群、混合环境下的云原生应用交付难题
  • 原文地址:https://www.cnblogs.com/seven7seven/p/3877789.html
Copyright © 2011-2022 走看看