您的位置:首页 > 其它

Hibernate第十三讲 学生课程分数映射

2013-03-20 21:52 513 查看
注:用hibernate自动生成的表存在bug,要先手工建表,在用hibernate去生生成

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐