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非常强大,所以 ...
随机推荐
- ThinkPHP下使用Ueditor
在做课程设计的时候想到用百度的Ueditor,可在配置的时候出现了一些问题 Ueditor感觉不是很难,以前有个人定制的,现在取消了这项服务,但是我们可以自己进行配置 下载地址:http://uedi ...
- art.dialog 与 ajax 异步请求
上周写了一些代码,涉及到jquery异步请求,这里归纳总结下,希望对刚接触编程的同学有帮助. 主要习惯使用 art.dialog 框架,非常好用,在异步请求上,它提供了很多简便的方法. 加载使用art ...
- 基于ARM-LINUX的温度传感器驱动(DS18B20) .
DS18B20数字温度传感器接线方便,封装成后可应用于多种场合,如管道式,螺纹式,磁铁吸附式,不锈钢封装式,型号多种多样,有LTM8877,LTM8874等等.主要根据应用场合的不同而改变其外观.封装 ...
- 回答了个问题,9x9 乘法表生成器
# -*- coding: utf-8 -*- from prettytable import PrettyTable pt = PrettyTable() # 需要安装prettytable这个库来 ...
- C++顺序容器类总结
主要是vector,deque,list,forward_list,array,string 插入方法: 元素访问: 元素删除: 容器赋值: forward_list有很多特殊的方法 毕竟平时forw ...
- [转贴] C/C++中动态链接库的创建和调用
DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的 ...
- picturebox 图片自适应
picturebox控件共有两种载入图片方式,分别为: pictureBox1.BackgroundImage = Image,pictureBox1.load(url) 为使加载的图片自使用控件尺寸 ...
- wcf 远程终结点已终止该序列 可靠会话出错
https://social.msdn.microsoft.com/Forums/office/zh-CN/9f0c76d2-85b0-4cd3-979d-ceda7947bcd1/-?forum=w ...
- VirtualBox的四种网络连接方式详解
VirtualBox中有4中网络连接方式: 1. NAT 2. Bridged Adapter 3. Internal 4. Host-only Adapter VMWare中有三种,其实他跟VMWa ...
- WordPress Videowall插件‘page_id’参数跨站脚本漏洞
漏洞名称: WordPress Videowall插件‘page_id’参数跨站脚本漏洞 CNNVD编号: CNNVD-201310-502 发布时间: 2013-10-23 更新时间: 2013-1 ...