hibernate--student_course_score
学生, 课程,分数的设计.
a)使用联合主键@EmbeddedId
使用Serializable接口
b)不适用联合主键
联合主键:
3张表,
student: id, name
course: id , name
score:studentid, courseid, score. (两个id就是联合ID)
不使用联合主键的话:
student:id, name
course: id, name
score: id,name, student, course
score--> student: many to one
course-->student: many to one
student->course: many to many
设计:
1. 实体类(表)
2. 导航 (为了编程方便)
3. 确定编程方式
Student.java:
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;
} }
Course.java:
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;
} }
Score.java:
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;
} }
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">linda0213</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:SXT</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
--> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">update</property>
-->
<!-- <mapping resource="com/bjsxt/hibernate/Group.hbm.xml"/>
<mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
--> <mapping class="com.bjsxt.hibernate.Student"/>
<mapping class="com.bjsxt.hibernate.Course"/>
<mapping class="com.bjsxt.hibernate.Score"/> </session-factory> </hibernate-configuration>
test文件:
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();
}
}
最后生成的score有问题, 变成了联合主键, 所以删除掉score表, 自己建一个:
use hibernate;
create table score(id int primary key auto_increment,student_id int references student(id),course_id int references course(id), score int)
然后注释掉beforeclass里的创建表语句再次执行就没问题了.
hibernate--student_course_score的更多相关文章
- Hibernate Student_Course_Score设计
示例: 设计代码,实现在数据库中建student表.course表.和score表,展现三者关系 student表:id.name course表:id.name score表:id.score.st ...
- hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用
问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...
- hibernate多对一双向关联
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Hibernate中事务的隔离级别设置
Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下
- Hibernate中事务声明
Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...
- spring applicationContext.xml和hibernate.cfg.xml设置
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- [原创]关于Hibernate中的级联操作以及懒加载
Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...
- hibernate的基本xml文件配置
需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...
- Maven搭建SpringMVC+Hibernate项目详解 【转】
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
随机推荐
- perl的logwrapper
来源: http://www.cnblogs.com/itech/archive/2012/09/22/2698385.html 对任何的函数将标准输出和错误输出重定向到对应的log文件. 对任何的函 ...
- wpa_supplicant wpa_cli 的使用说明
wpa_supplicant -d -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf -iwlan0 -B 会在/data/misc/wifi/下产生 ...
- libprotobuff8.so not found
http://stackoverflow.com/questions/25518701/protobuf-cannot-find-shared-libraries
- 倒计时demo
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (strong,nonatom ...
- vim 设置默认显示行号
vim 要默认打开的时候显示行号,这样就可以不用每次编辑的时候在手动在命令模式下:set number 来显示行号. 具体做法是: 创建一个 .vimrc的配置文件. 在启动vim时,当前用户根目录下 ...
- document.querySelectorAll遍历(forEach小解)
document.querySelectorAll兼容性良好,在之前的项目中就其遍历方式出了错误,先做个小结: 1.for循环 传统遍历方法 for(var i= 0; i< document. ...
- java 接口的回调
Example6_3.java interface ShowMessage { void 显示商标(String s); } class TV implements ShowMessage { pub ...
- itext操作PDF文件添加水印
功能描述:添加图片和文字水印 /** * * [功能描述:添加图片和文字水印] [功能详细描述:功能详细描述] * @param srcFile 待加水印文件 * @param destFile 加水 ...
- Nginx配置-伪静态,隐藏index.php大入口
server { listen ; server_name ; root E:/www/wvtuan/; index index.php index.html; log_not_found off; ...
- GPRS DTU概念及DTU的工作原理(转)
源:http://blog.csdn.net/bichenggui/article/details/7889638 最近需要开发一个基于GRPS DTU数据传输的数据中心方案,于是找了一些资料.个人觉 ...