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的就留在下一篇吧,这 ...
随机推荐
- C、C++的Makefile的编写以及动、静态库的制作调用(包括MAC地址的获取及MD5加密)
一.C代码 静态库 四个.h.c文件 add.h #ifndef ADD_H #define ADD_H int add(int a,int b); #endif add.c #include < ...
- Inno Setup入门(七)——提供安装语言选项
Inno Setup安装目录下有一个Languages的文件夹,该文件夹提供了可供使用的语言,通过在脚本中加入[languages]段,可以实现该项功能,实现代码如下: [setup] ;全局设置,本 ...
- nginx 正则表达式
1.nginx配置基础 1.正则表达式匹配 ~ 区分大小写匹配 ~* 不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 ^ 以什么开头的匹配 $ 以什么结尾的匹配 转义字符.可 ...
- Spring Boot 系列教程17-Cache-缓存
缓存 缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找.由于缓存的运行速度比内存快得多,故缓存的作用就是帮 ...
- Notes over compiling..
When compiling VIM on windows, using nmake may be a better choice.. Because so far my attempts to co ...
- JS-运动基础(一)
<title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red; posit ...
- 记一次坑die(误)的题目--HDU2553--(DFS)
,N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- java 设计模式之工厂模式与反射的结合
工厂模式: /** * @author Rollen-Holt 设计模式之 工厂模式 */ interface fruit{ public abstract void eat(); } ...
- Git学习 -- 工作区和暂存区
工作区(working directory): 就是能看到的目录,如我的git文件夹 版本库(repository): 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库 里面最重要的就 ...
- Maven常用插件配置和使用
主要介绍Maven的几个常见第三方插件(cobertura.findbugs.source.assembly.插件开发)配置和使用,接Maven介绍 maven本质上是一个插件框架,它的所有工作都交给 ...