Hibernate学习之表一对多,多对一关系
代码:
person类:
public class Person { private long id; private String name; private int age; private Date birthDay; private int schNo; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Date getBirthDay() {
return birthDay;
} public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
} public int getSchNo() {
return schNo;
} public void setSchNo(int schNo) {
this.schNo = schNo;
} public Person() {
super();
} public Person(long id, String name, int age, Date birthDay, int schNo) {
super();
this.id = id;
this.name = name;
this.age = age;
this.birthDay = birthDay;
this.schNo = schNo;
} @Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", birthDay=" + birthDay + ", schNo=" + schNo + "]";
}
}
school类:
public class School { private int schNo; private String schName; private Set<Person> persons; public int getSchNo() {
return schNo;
} public void setSchNo(int schNo) {
this.schNo = schNo;
} public String getSchName() {
return schName;
} public void setSchName(String schName) {
this.schName = schName;
} public School(int schNo, String schName) {
super();
this.schNo = schNo;
this.schName = schName;
} public Set<Person> getPersons() {
return persons;
} public void setPersons(Set<Person> persons) {
this.persons = persons;
} public School() {
super();
}
}
test.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="hibernate.entity.Person" table="person">
<id name="id" column="pid"></id>
<property name="name" column="pname"></property>
<property name="birthDay" type="date" ></property>
<property name="age"></property>
<many-to-one name="schNo" column="schNo" class="hibernate.entity.School"/>
</class> <class name="hibernate.entity.School" table="school">
<id name="schNo" column="schNo">
<generator class="native"></generator>
</id>
<property name="schName" column="schName"></property>
<set name="persons">
<key column="schNo"/>
<one-to-many class="hibernate.entity.Person"/>
</set>
</class>
</hibernate-mapping>
测试:
创建表并导出建表语句
@Test
public void test8(){
Configuration config = new Configuration().configure("test.hbmcfg.xml");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.setOutputFile("D:"+File.separator+"test"+File.separator+"hibrenate_table.sql");
schemaExport.setFormat(true);
schemaExport.create(true, true);
//schemaExport.execute(true, true, false, true);
}
结果:
drop table person cascade constraints drop table school cascade constraints drop sequence hibernate_sequence create table person (
pid number(19,0) not null,
pname varchar2(255 char),
birthDay date,
age number(10,0),
schNo number(10,0),
primary key (pid)
) create table school (
schNo number(10,0) not null,
schName varchar2(255 char),
primary key (schNo)
) alter table person
add constraint FKC4E39B55D836733C foreign key (schNo) references school
create sequence hibernate_sequence
Hibernate学习之表一对多,多对一关系的更多相关文章
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...
- Hibernate 、多表关联映射-多对一关系(many-to-one)
Hibernate.cfg.xml: <session-factory name="sessionFactory"> <property name="h ...
- SSAS中事实表中的数据如果因为一对多或多对多关系复制了多份,在维度上聚合的时候还是只算一份
SSAS事实表中的数据,有时候会因为一对多或多对多关系发生复制变成多份,如下图所示: 图1 我们可以从上面图片中看到,在这个例子中,有三个事实表Fact_People_Money(此表用字段Money ...
- JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析
JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...
- 数据表设计:多对多关系E-R图转换——中间表
链接:https://blog.csdn.net/vainfanfan/article/details/80568784 链接2:https://www.cnblogs.com/hiwangzi/p/ ...
- Hibernate学习(四)———— 双向多对多映射关系
一.小疑问的解答 问题一:到这里,有很多学习者会感到困惑,因为他不知道使用hibernate是不是需要自己去创建表,还是hibernate全自动,如果需要自己创建表,那么主外键这种设置也是自己设置吗? ...
- hibernate中一对多多对一关系设计的理解
1.单向多对一和双向多对一的区别? 只需要从一方获取另一方的数据时 就使用单向关联双方都需要获取对方数据时 就使用双向关系 部门--人员 使用人员时如果只需要获取对应部门信息(user.getdept ...
- Hibernate关联映射(一对多/多对多)
版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/843401053. Hibernate关联映射上接Hibernate持久化类:h ...
- Hibernate学习---单表查询
我们都知道SQL是非常强大的,为什么这么说呢?相信学过数据库原理的同学们都深有体会,SQL语句变化无穷,好毫不夸张的说可以实现任意符合我们需要的数据库操作,既然前面讲到Hibernate非常强大,所以 ...
随机推荐
- sqlsever2008数据库的备份与还原
本文数据库的名称为ProjectControl public static SqlConnection conn = new SqlConnection("server=(local);u ...
- linux的fork函数
fork函数 头文件:#include<unistd.h> 函数原型:pid_t fork( void);(pid_t 是一个宏定义,其实质是int 被定义在#include< ...
- linux中C的静态库和动态库分析
从开始学C语言写第一个"hello world"历程到现在,我依然困惑于到底这个程序完整的执行流程是什么样的.不过,现在我正在尝试一点一点的揭开它的面纱.现在,我尝试分析linux ...
- WARNING L15: MULTIPLE CALL TO SEGMENT
原网页:http://www.cnblogs.com/CuriosityWzk/archive/2011/12/25/2301090.html WARNING L15: MULTIPLE CALL T ...
- JavaScript的OOP编程1
首先要说的是,javascript其实是可以进行OOP编程的,其次javascript的OOP编程实现方式有多种,我写的这一种只是我测试过,可行的一种 version1 // 父类 function ...
- 2D image convolution
在学习cnn的过程中,对convolution的概念真的很是模糊,本来在学习图像处理的过程中,已对convolution有所了解,它与correlation是有不同的,因为convolution = ...
- skiplist 跳表(1)
最近学习中遇到一种新的数据结构,很实用,搬过来学习. 原文地址:skiplist 跳表 为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. ...
- Ajax、Comet、HTML 5 Web Sockets技术比较分析
最近因为考虑研究B/S结构网站即时消息处理 参考了 JAVA怎么样实现即时消息提醒http://bbs.csdn.net/topics/330015611http://www.ibm.com/deve ...
- nutch 索引
nutch开发环境搭建 nutch-1.3导入eclipse nutch-1.7导入eclipse nutch部署 nutch-1.3linux下部署 nutch-1. ...
- Entity FrameWork知识点汇总
这里罗列的并非EF的所有知识点 ,只是我在开发过程中遇到或者使用到的知识,记录于此, 备忘 1:EF的三种创建方式 A:Database First B:Model First C:Code Firs ...