hibernate 映射实例 学生 课程 成绩

学生和课程是多对多,一个学生的一个课程只能对应一个成绩。

所以学生和课程多对多,其中间表多了一个成绩字段。

可以这样设计:

学生和课程通过中间表--成绩,多对多映射。

 

 

手动建中间表语句:

create table score(id int primary key auto_increment,student_id int refrences student(id),course_id int references course(id),score int);

 

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@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;
    }
   
}

 

 

package com.bjsxt.hibernate;

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

@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;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    @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;
    }
   
}

 

 

package com.bjsxt.hibernate;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

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

 

 

测试类

package com.bjsxt.hibernate;

import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateTreeTest {
    private static SessionFactory sessionFactory;
   
    @BeforeClass
    public static void beforeClass() {
        //new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    @AfterClass
    public static void afterClass() {
        sessionFactory.close();
    }
   
    @Test
    public void testSave() {
        Student s = new Student();
        s.setName("zhangsan");
        Course c = new Course();
        c.setName("java");
        Score score = new Score();
        score.setCourse(c);
        score.setStudent(s);
       
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(s);
        session.save(c);
        session.save(score);
   
        session.getTransaction().commit();
        session.close();
    }
    @Test
    public void testLoad() {
        testSave();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
       
        Student s = (Student)session.load(Student.class, 1);
        for(Course c : s.getCourses()) {
            System.out.println(c.getName());
        }
       
        session.getTransaction().commit();
        session.close();
       
    }
   
   
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
    }
   
   
    public static void main(String[] args) {
        beforeClass();
    }
}

posted @ 2013-12-18 14:32  剑握在手  阅读(436)  评论(0编辑  收藏  举报
返回顶部↑