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非常强大,所以 ...
随机推荐
- 【python】【转】if else 和 elif
else和elif语句也可以叫做子句,因为它们不能独立使用,两者都是出现在if.for.while语句内部的.else子句可以增加一种选择:而elif子句则是需要检查更多条件时会被使用,与if和els ...
- 程序员老鸟写sql语句的经验之谈
做管理系统的,无论是bs结构的还是cs结构的,都不可避免的涉及到数据库表结构的设计,sql语句的编写等.因此在开发系统的时候,表结构设计是否合理,sql语句是否标准,写出的sql性能是否优化往往会成为 ...
- combo下拉列表选择
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 偶尔转帖:AI会议的总结(by南大周志华)
偶尔转帖:AI会议的总结(by南大周志华) 说明: 纯属个人看法, 仅供参考. tier-1的列得较全, tier-2的不太全, tier-3的很不全. 同分的按字母序排列. 不很严谨地说, tier ...
- ubuntu 10.04 安装qt 5.0.2
转自ubuntu 10.04 安装qt 5.0.2 从qt project网站下载下来最新的qt5.0.2套件,发现是个.run文件,添加x属性,然后直接sudo ./****.run, 提示 /l ...
- 【BZOJ 3529】 [Sdoi2014]数表 (莫比乌斯+分块+离线+树状数组)
3529: [Sdoi2014]数表 Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和j的所有 ...
- poj crane
#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #de ...
- [wikioi]线段覆盖 2
http://wikioi.com/problem/3027/ # 有个小错误调了半天,最终发现sort(line, line+N)错了,后面那个是exclusive的,所以要line+N+1.# 按 ...
- Android软件开发之获取通讯录联系人信息
Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来. 这一篇文章我主要带领同学们熟悉Android的通讯录机制. 图中选中 ...
- Navigation Drawer介绍
在2013 google IO当天,Android团的更新了Support库,新版本(V13)的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建 Nav ...