代码:

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学习之表一对多,多对一关系的更多相关文章

  1. Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)

    hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...

  2. Hibernate 、多表关联映射-多对一关系(many-to-one)

    Hibernate.cfg.xml: <session-factory name="sessionFactory"> <property name="h ...

  3. SSAS中事实表中的数据如果因为一对多或多对多关系复制了多份,在维度上聚合的时候还是只算一份

    SSAS事实表中的数据,有时候会因为一对多或多对多关系发生复制变成多份,如下图所示: 图1 我们可以从上面图片中看到,在这个例子中,有三个事实表Fact_People_Money(此表用字段Money ...

  4. JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析

    JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...

  5. 数据表设计:多对多关系E-R图转换——中间表

    链接:https://blog.csdn.net/vainfanfan/article/details/80568784 链接2:https://www.cnblogs.com/hiwangzi/p/ ...

  6. Hibernate学习(四)———— 双向多对多映射关系

    一.小疑问的解答 问题一:到这里,有很多学习者会感到困惑,因为他不知道使用hibernate是不是需要自己去创建表,还是hibernate全自动,如果需要自己创建表,那么主外键这种设置也是自己设置吗? ...

  7. hibernate中一对多多对一关系设计的理解

    1.单向多对一和双向多对一的区别? 只需要从一方获取另一方的数据时 就使用单向关联双方都需要获取对方数据时 就使用双向关系 部门--人员 使用人员时如果只需要获取对应部门信息(user.getdept ...

  8. Hibernate关联映射(一对多/多对多)

    版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/843401053.  Hibernate关联映射上接Hibernate持久化类:h ...

  9. Hibernate学习---单表查询

    我们都知道SQL是非常强大的,为什么这么说呢?相信学过数据库原理的同学们都深有体会,SQL语句变化无穷,好毫不夸张的说可以实现任意符合我们需要的数据库操作,既然前面讲到Hibernate非常强大,所以 ...

随机推荐

  1. 设置Android程序的默认安装位置

    修改 AndroidManifest.xml 文件: <manifest xmlns:android="http://schemas.android.com/apk/res/andro ...

  2. sql server 与C#数据类型对应表

  3. 【原】jQuery编写插件

    分享一下编写设置和获取颜色的插件,首先我将插件的名字命名为jquery.color.js.该插件用来实现以下两个功能1.设置元素的颜色.2.获取元素的颜色. 先在搭建好如下编写插件的框架: ;(fun ...

  4. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

  5. Linux用户环境变量

    Linux用户环境变量 环境变量就是系统或软件设置的一些参数,用户环境变量就是用户登录系统后,都有自已专用的运行环境.在Windows系统中用户环境变量保存在用户家目录,Linux也是同样的.本文主要 ...

  6. crontab 中 python(cx_Oracle)脚本执行时需要用户环境变量,怎么办??

    import cx_Oracle Traceback (most recent call last): File "", line 1, in ? ImportError: lib ...

  7. linux和windows双系统导致的时间日

    我的博客:www.while0.com系统中有两种时间区分,一为UTC,另一为LT(地方时)两者的区别为时区不同,UTC就是0时区的时间,而我们当地是用的北京时间要慢8小时.linux采用的UTC时间 ...

  8. python-os.walk目录递归

    递归删除文件或目录: 递归,os.walk 删除目录,shutil.rmtree # -*- coding: UTF-8 -*- import os.path,sys,shutil path = 'H ...

  9. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  10. linux使用crontab -e 遇到No space left on device

    今天用linux的crontab -e编辑定时脚本的时候.遇到No space left on device的错误. 网上找了半天终于知道原因了,昨天晚上我的一个任务因为路径没写对,到时crontab ...