hibernate建表多对多建表
Student.java
package cn.itcast.hiberate.sh.domain;
import java.util.Set;
public class Student {
private Long sid;
private String sname;
private String description;
Set<Course> courses;
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
public Long getSid() {
return sid;
}
public void setSid(Long sid) {
this.sid = sid;
}
public Student(String sname, String description) {
super();
this.sname = sname;
this.description = description;
}
public Student() {
// TODO Auto-generated constructor stub
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.hiberate.sh.domain.Student">
<id name="sid" length="5">
<generator class="increment"></generator>
</id>
<property name="sname" length="20"></property>
<property name="description" length="100"></property>
<!--
table就是用来描述第三张表
key 用来描述外键,多对多的关系看对哪张表建立映射
此表是描述Student信息的,所以 key中 column写sid many-to-many 中用于建立关系的所以 在第三张表中关系用cid建立
-->
<set name="courses" table="student_course">
<key>
<column name="sid"></column>
</key>
<many-to-many class="cn.itcast.hiberate.sh.domain.Course" column="cid">
</many-to-many >
</set>
</class>
</hibernate-mapping>
Course.java
package cn.itcast.hiberate.sh.domain; import java.io.Serializable;
import java.util.Set; public class Course implements Serializable {
private Long cid;
private String cname;
private String description;
private Set<Student> students;
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
} }
Course.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.hiberate.sh.domain.Course">
<id name="cid" length="5" type="java.lang.Long">
<generator class="increment"></generator>
</id>
<property name="cname" length="20" type="java.lang.String"></property> <property name="description" length="100" type="java.lang.String"></property>
<!--
set元素对应类中的set集合
通过set元素使classes表与student表建立关联
key是通过外键的形式让两张表建立关联,针对的是哪张表看哪个
one-to-many是通过类的形式让两个类建立关联 cascade 级联
save-update
1、当 保存班级的时候,对学生进行怎么样的操作
如果学生对象在数据库中没有对应的值,这个时候会执行save操作
如果学生对象在数据库中有对应的值,这个时候会执行update操作
delete
all
inverse 维护关系
true 不维护关系
false 维护关系
default false
-->
<set name="students" table="student_course">
<!--
key是用来描述外键 哪张表就哪個外鍵
<many-to-many> 中的cid 描述类与类之间的关系
-->
<key>
<column name="cid"></column>
</key>
<many-to-many class="cn.itcast.hiberate.sh.domain.Student" column="sid">
</many-to-many>
</set>
</class>
</hibernate-mapping>
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只能连接一个数据库
-->
<session-factory>
<!--
数据库的用户名
-->
<property name="connection.username">root</property>
<!--
密码
-->
<property name="connection.password">friends</property>
<!--
url
-->
<property name="connection.url">
jdbc:mysql://localhost:3306/itcast_sh_hibernate_manyTomany
</property>
<!--
作用:根据持久化类和映射文件生成表
validate
create-drop
create
update
-->
<property name="hbm2ddl.auto">update</property>
<!--
显示hibernate内部生成的sql语句
--> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="cn/itcast/hiberate/sh/domain/Course.hbm.xml" />
<mapping resource="cn/itcast/hiberate/sh/domain/Student.hbm.xml" /> </session-factory>
</hibernate-configuration>
test.java
package cn.itcast.hiberate.sh.test; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; import cn.itcast.hibernate.sh.utils.HiberanteUtils; public class CreateTable extends HiberanteUtils{ @Test
public void createTable()
{
Configuration config=new Configuration();
config.configure();
// SessionFactory sessionFactory=config.buildSessionFactory();
// Session session=sessionFactory.openSession();
// Transaction transaction=session.beginTransaction();
//
//
// transaction.commit();
// session.close();
}
}
测试2
package cn.itcast.hibernate.sh.test; import java.util.HashSet;
import java.util.List;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test; import cn.itcast.hiberate.sh.domain.manytomany.Course;
import cn.itcast.hiberate.sh.domain.manytomany.Student;
import cn.itcast.hibernate.sh.utils.HiberanteUtils; /**
* 1、新建一个课程
* 2、新建一个学生
* 3、新建课程的同时新建学生 级联
* 4、已经存在一个课程,新建一个学生,建立课程和学生之间的关系
* 5、已经存在一个学生,新建一个课程,建立课程和学生之间的关系
* 6、已经存在一个课程,已经存在一个学生,建立关联
* 7、把已经存在的一些学生加入到已经存在的一个课程中
* 8、把一个学生加入到一些课程中
* 9、把多个学生加入到多个课程中
* 10、把一个已经存在的学生从一个已经的课程中移除
* 11、把一些学生从一个已经存在的课程中移除
* 12、把一个学生从一个课程转向到另外一个课程
* 13、删除课程
* 14、删除学生
* @author Think
*
*/
public class ManyToManyTest extends HiberanteUtils{
static{
url = "cn/itcast/hiberate/sh/domain/manytomany/hibernate.cfg.xml";
}
@Test
public void testCreateTable(){ } @Test
public void testSaveStudent_Cascade_Course_Save(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
student.setSname("班长");
student.setDescription("牛人"); Course course = new Course();
course.setCname("生理卫生");
course.setDescription("很好"); Set<Course> courses = new HashSet<Course>();
courses.add(course);
student.setCourses(courses);
session.save(student);
transaction.commit();
session.close();
} /**
* 已经存在一个课程,新建一个学生,建立课程和学生之间的关系
* 从课程角度出发
*/
@Test
public void testUpdateCourse_Cascade_Student_Save_R(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course)session.get(Course.class, 1L);
Student student = new Student();
student.setSname("班迷");
student.setDescription("班丝:班长的钢丝");
course.getStudents().add(student);
session.save(student);
transaction.commit();
session.close();
} /**
* 已经存在一个课程,新建一个学生,建立课程和学生之间的关系
* 从学生角度出发
*/
@Test
public void testSaveStudent_R(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course)session.get(Course.class, 1L);
Student student = new Student();
student.setSname("班迷");
student.setDescription("班丝:班长的钢丝");
Set<Course> courses = new HashSet<Course>();
courses.add(course);
student.setCourses(courses);
session.save(student);
transaction.commit();
session.close();
} /**
* 已经存在一个课程,已经存在一个学生,建立关联
*/
@Test
public void testR(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course)session.get(Course.class, 1L);
Student student = (Student)session.get(Student.class, 2L);
student.getCourses().add(course);
transaction.commit();
session.close();
} /**
* 把学生3,4加入到课程1中
*/
@Test
public void testR_Some(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Course course = (Course)session.get(Course.class, 1L);
Student student = (Student)session.get(Student.class, 3L);
Student student2 = (Student)session.get(Student.class, 4L);
124 //注释的语句效率不如没有注释的语句,因为hibernate存在缓存,对于不同的对象student.getcourse()查询了两次sql语句
125 //而course.getStudent()则之执行一次查询语句
// student.getCourses().add(course);
// student2.getCourses().add(course);
course.getStudents().add(student2);
course.getStudents().add(student);
transaction.commit();
session.close();
} /**
* 把一个学生加入到一些课程中
*/
@Test
public void testR_Some_2(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = (Student)session.get(Student.class, 3L);
List<Course> courseList = session.createQuery("from Course where cid in(1,2,3)").list();
student.getCourses().addAll(courseList);
transaction.commit();
session.close();
} /**
* 把学生从一个课程转移到另外一个课程
*/
@Test
public void testTransform(){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Student student = (Student) session.get(Student.class, 3L);
Course course1 = (Course)session.get(Course.class, 1L);
Course course3 = (Course)session.get(Course.class, 3L);
student.getCourses().remove(course1);
student.getCourses().add(course3);
transaction.commit();
session.close();
}
}
hibernate建表多对多建表的更多相关文章
- hibernate 一对多 多对一 关系表 增删改查大礼包ps二级查也有
今天来到混元气功 这货大概的意思就是你中有我 我中有你 ps 这里就要说到维护关系 ps写这个用了我一下午.......也是刚刚好复习到这里 顺便就写写 注意:一般都在多方维护关系,至于是用单向还是用 ...
- Day055--MySQL--外键的变种,表与表的关系,单表查询,多表查询, 内连接,左右连接,全外连接
表和表的关系 ---- 外键的变种 * 一对多或多对一 多对多 一对一 参考 https://www.cnblogs.com/majj/p/9169416.html 如何找出两张表之间的关系 分析步骤 ...
- mysql 表关系 与 修改表结构
目录 mysql 表关系 与 修改表结构 两张表关系 分析步骤 修改表结构 mysql 表关系 与 修改表结构 两张表关系 多对一 以员工和部门举例 多个员工对应一个部门 foreign key 永远 ...
- 配置hibernate根据实体类自动建表功能
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<propert ...
- 【原创】Hibernate通过实体类自动建表时type=MyISAM的问题
ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 当使用的mysql数据库为5.5版本时,方言需要设置为 <property name="hibernate.dialect&q ...
- Hibernate+maven+eclipse 实现自动建表
一.需求 如题,当建好Model 时,不想自己手工建表,可以采取hibernate进行自动建表.下面将用一个小例子来说明如何将其实现. 二.实现 说明:1)这里用的是4.3.1.Final版本的hib ...
- 【SSH】——Hibernate实现简单的自动建表
[与ORM] Object Relational Mapping,对象关系映射,将对象和关系联系了起来.面向对象是从耦合.聚合.封装等的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论 ...
- day58_9_24多对多建表手动,form组件(判断类型),cookies和session
一.多对多建表关系之手动添加. 1.全自动 像之前讲过的一样,我们可以通过manytomanyField的字段来建立多对多关系: class Book(models.Model): title = m ...
- hibernate中先建表还是先建实体类
在实际工作中往往是先建表然后再生成类原因:建好数据库表之后往往要对数据表进行一些优化,比如说建索引,比如说建中间表,比如建视图.如果先建类的话这些优化是无法生成的
随机推荐
- C语言关键字
No. 关键字 意义 备注 1 auto 声明自动变量 2 break 跳出当前循环 3 case switch语句的分支 4 char 声明字符型变量 5 const 声明只读变量 C90新增 6 ...
- malloc/free 和 new/delete 的区别
1.malloc在C和C++中的区别 1.1.C中可以将任意的指针赋值给void*类型的变量.也可以将void*类型的值赋值给通常的指针变量. ---------------------------- ...
- SinoSure
Sino,就是“中国.东方”的意思, 这个词只能作为前缀使用,不能单独讲.西方社会有时使用“Sino-”来表示“中国(的)”的意思,但是“Sino”均为连接词,并非单独用来表示“中国”之语.如表达中美 ...
- 为什么要关闭360云盘:新来的美工嫌我们logo太丑,所以就决定关闭了。这个理由怎么样
新来的美工嫌我们logo太丑,所以就决定关闭了.这个理由怎么样曾经拥有的不要忘记:不能得到的更要珍惜:属于自己的不要放弃:已经失去的留作回忆.我刚来~~~嘿嘿~~ 久经考验的,忠诚的国际宅男主义战士, ...
- Mysql Workbench 学习
1.安装 http://dev.mysql.com/downloads/tools/workbench/ 选择合适的,下载(以Ubuntu 为例) cd到下载目录,然后sudo dpkg -i wor ...
- iOS sqlite3数据库解析
看来从版本3.3.1基本上已经支持线程句柄的传递功能.具体限制我标记了一下.(6) Is SQLite threadsafe?SQLite is threadsafe. We make this co ...
- JVM垃圾回收机制总结(5) :JDK垃圾收集器的配置命令
以下配置主要针对分代垃圾回收算法而言. 堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理 ...
- iOS:UIView的block函数实现转场动画---双视图
使用UIView动画函数实现转场动画——双视图 + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView durati ...
- hive0.13网络接口安装
安装好hive 0.13以后,在./lib下找不到hive-hwi-0.13.1.war ,那该怎么办? 1.下载hive-0.12.0版本,把这一版里面的hive-hwi-0.12.0.war重 ...
- H5移动前端完美布局之padding
序上次的提到了H5移动前端完美布局之-margin百分比的使用margin-top(left,right,bottom)的百分比在移动页面布局中对上下左右距离的处理,攻下城外再攘城内,今天看看padd ...