【hibernate框架】练习-学生、课程、分数的设计(重要)

学生、课程、分数的设计(重要)

表设计:
方法1:
设置联合主键@EmbeddedId
实现Serializable接口

student:id<int> name<String>
course:id<int> name<String>
score:studentid<int> courseid<int> score<int>//联合主键联合id
//有点麻烦不写了,直接看方法2吧

方法2:
student:id<int> name<String>
course:id<int> name<String>
score:id<int> studentid<int> courseid<int> score<int>//这一方设ManyToOne

实现代码:
Student.java:
package cn.edu.hpu.testSchool;

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;
import javax.persistence.Table;


@Entity
@Table(name="s_student")
public class Student {
	private int id;
	private String name;
	private Set<Course> courses=new HashSet<Course>();
	
	@ManyToMany
	@JoinTable(name="s_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;
	}
	@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;
	}
	
}



Score.java:
package cn.edu.hpu.testSchool;

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="s_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="studnet_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;
	}
	
}

Course.java:
package cn.edu.hpu.testSchool;


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


@Entity
@Table(name="s_course")
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;
	}
	
}

在hibernate.cfg.xml配置:
<mapping class="cn.edu.hpu.testSchool.Student"/> 
<mapping class="cn.edu.hpu.testSchool.Score"/>
<mapping class="cn.edu.hpu.testSchool.Course"/>

建表语句:
  create table s_course (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    create table s_score (
        id integer not null,
        score integer not null,
        course_id integer,
        student_id integer not null auto_increment,
        primary key (student_id, course_id)
    )


    create table s_student (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )


    alter table s_score 
        add index FK6C0C3FC68B2C002E (student_id), 
        add constraint FK6C0C3FC68B2C002E 
        foreign key (student_id) 
        references s_student (id)


    alter table s_score 
        add index FK6C0C3FC6EFAFE106 (course_id), 
        add constraint FK6C0C3FC6EFAFE106 
        foreign key (course_id) 
        references s_course (id)


结果:
student:id<int> name<String>
course:id<int> name<String>
score:id<int> studentid<int> courseid<int> score<int>


测试添加:
@Test
public void testSchoolAdd(){
	cn.edu.hpu.testSchool.Student s=new cn.edu.hpu.testSchool.Student();
	s.setName("zhangsan");
	Course c=new Course();
	c.setName("java");
	Score score=new Score();
	score.setCourse(c);
	score.setStudent(s);
	score.setScore(80);
	
	
	sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	Session session=sessionFactory.openSession();
	session.beginTransaction();
	session.save(s);
	session.save(c);
	session.save(score);
	session.getTransaction().commit();
	session.close();
}


结果:
Hibernate: 
    insert 
    into
        s_student
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        s_course
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        s_score
        (course_id, score, student_id) 
    values
        (?, ?, ?)


测试取数据:
@Test
public void testSchoolLoad(){


	sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
	Session session=sessionFactory.openSession();
	session.beginTransaction();
	cn.edu.hpu.testSchool.Student s=(cn.edu.hpu.testSchool.Student)session.load(cn.edu.hpu.testSchool.Student.class, 2);
	for(Course c:s.getCourses()){
		System.out.println(c.getName());
	}
	session.getTransaction().commit();
	session.close();
}

结果:
Hibernate: 
    select
        student0_.id as id0_0_,
        student0_.name as name0_0_ 
    from
        s_student student0_ 
    where
        student0_.id=?
Hibernate: 
    select
        courses0_.student_id as student4_0_1_,
        courses0_.course_id as course3_1_,
        course1_.id as id2_0_,
        course1_.name as name2_0_ 
    from
        s_score courses0_ 
    inner join
        s_course course1_ 
            on courses0_.course_id=course1_.id 
    where
        courses0_.student_id=?

java

转载请注明出处:http://blog.csdn.net/acmman/article/details/43917081

你可能感兴趣的:(Hibernate,框架,表的设计)