【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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值