hibernate--student_course_score

时间:2022-09-21 13:38:00

学生, 课程,分数的设计.

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的更多相关文章

  1. Hibernate Student&lowbar;Course&lowbar;Score设计

    示例: 设计代码,实现在数据库中建student表.course表.和score表,展现三者关系 student表:id.name course表:id.name score表:id.score.st ...

  2. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  3. 解决 Springboot Unable to build Hibernate SessionFactory &commat;Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  4. hibernate多对一双向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  5. Hibernate中事务的隔离级别设置

    Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下

  6. Hibernate中事务声明

    Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...

  7. spring applicationContext&period;xml和hibernate&period;cfg&period;xml设置

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  8. &lbrack;原创&rsqb;关于Hibernate中的级联操作以及懒加载

    Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...

  9. hibernate的基本xml文件配置

    需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...

  10. Maven搭建SpringMVC&plus;Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

随机推荐

  1. Java总结篇系列:Java 反射

    Java反射: package com.corn; import java.lang.reflect.Constructor; import java.lang.reflect.Field; impo ...

  2. js获取浏览器语言(ie、ff、chrome)、contextpath

    /js获取浏览器语言(ie.ff.chrome) var language_en_us = "en-us"; var language_zh_cn = "zh-cn&qu ...

  3. 利用doScroll在IE浏览器里模仿DOMContentLoaded

    稍微了解一点框架的事件绑定的都知道 window.onload 事件需要在页面所有内容(包括图片.flash.iframe等)加载完后,才执行,但往往我们更希望在 DOM 一加载完就执行脚本,而各大框 ...

  4. C&num;网站实现QQ第三方登陆&num; C&num;快速开发教程

    C#网站实现QQ第三方登陆 说起在网站上面可以直接使用QQ登录功能大家并不陌生.但翻其官方提供的SDK包中却没有C#方向的. 但是我们有个牛人叫张善友,做了一个民间SDK.下面我们就是用他所写的SDK ...

  5. tomcat https 未测试成功的版本

  6. Android SharedPreferences复杂的存储

    我们知道SharedPreferences简单类型的数据.比如.String.int等. 假设想用SharedPreferences存取更复杂的数据类型(类.图像等),就须要对这些数据进行编码. 我们 ...

  7. ios WKWebView 与 JS 交互实战技巧

    一.WKWebView 由于Xcode8发布之后,编译器开始不支持iOS 7了,这样我们的app也改为最低支持iOS 8.0,既然需要与web交互,那自然也就选择使用了 iOS 8.0之后 才推出的新 ...

  8. C&num; 封装SqlBulkCopy,让批量插入更方便

    关于 SqlServer 批量插入的方式,前段时间也有大神给出了好几种批量插入的方式及对比测试(http://www.cnblogs.com/jiekzou/p/6145550.html),估计大家也 ...

  9. MySQL数据库视图&lpar;view&rpar;,视图定义、创建视图、修改视图

    原文链接:https://blog.csdn.net/moxigandashu/article/details/63254901

  10. MyEclipse常用设置和快捷键

    Java快捷键 1.注释快捷键 先敲/  再敲两个**     Enter 回车 2.system.out.println(); 常用设置 [子类继承父类] [编码字体设置] 删除当前行:ctrl+d ...