首先,建立user表,news表

建立User,News类

package com.example.hibernate;

import java.util.Set;

public class User {

    private int id;
private String name;
private String head_image;
private String mobile;
private String email;
private String address;
private int age;
private String user_no;
private String password;
private Set<News> newsSet; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHead_image() {
return head_image;
}
public void setHead_image(String head_image) {
this.head_image = head_image;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUser_no() {
return user_no;
}
public void setUser_no(String user_no) {
this.user_no = user_no;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<News> getNewsSet() {
return newsSet;
}
public void setNewsSet(Set<News> newsSet) {
this.newsSet = newsSet;
} }
package com.example.hibernate;

import java.util.Date;

public class News {

    private int id;
private String title;
private User user; public News(String title) {
super();
this.title = title;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} }

配置映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.example.hibernate.User" table="user" > <id name="id">
<generator class="native"/>
</id> <property name="name" column="name" />
<property name="head_image" column="head_image" />
<property name="mobile" column="mobile" />
<property name="email" column="email" />
<property name="address" column="address" />
<property name="age" column="age" />
<property name="user_no" column="user_no" />
<property name="password" column="password" /> <set name="newsSet" table="news" cascade="save-update,delete">
<key column="user_id"></key>
<one-to-many class="com.example.hibernate.News"/>
</set>
</class> </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.example.hibernate.News" table="news"> <id name="id">
<generator class="native"/>
</id> <property name="title" column="title" /> <many-to-one name="user" column="user_id" class="com.example.hibernate.User"></many-to-one> </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>
<!--url信息 -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernte_test</property>
<!--数据库方言信息-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--密码 -->
<property name="connection.password">1111</property>
<!--数据库驱动信息 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.show_sql">true</property> <!--指定Hibernate映射文件路径 -->
<mapping resource="com/example/hibernate/User.hbm.xml" />
<mapping resource="com/example/hibernate/News.hbm.xml" />
</session-factory>
</hibernate-configuration>

编写测试类:

package com.example.hibernate;

import java.util.HashSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class MyTest { SessionFactory sessionFactory;
Session session;
Transaction transaction; @Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
} @Test
public void save(){
User user = new User();
user.setAddress("福建厦门鼓浪屿");
user.setAge(10);
user.setEmail("1233454543@zzu.cn");
user.setHead_image("ïmg1.jpg");
user.setMobile("123224324");
user.setName("王天才");
user.setPassword("11111");
user.setUser_no("11111");
user.setNewsSet(new HashSet<News>()); News news1 = new News("测试一");
News news2 = new News("测试二"); news1.setUser(user);
news2.setUser(user); user.getNewsSet().add(news1);
user.getNewsSet().add(news2);

//会多执行多余语句,切记需要添加inverse(本程序未添加)
session.save(user);
} @After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
} }

hibernate关联映射的更多相关文章

  1. Hibernate关联映射关系

    Hibernate关联映射关系 一.双向一对多关联映射关系:当类与类之间建立了关联,就可以方便的从一个对象导航到另一个或另一组与它关联的对象(一对多双向关联和多对一双向关联是完全一样的) 1.1创建实 ...

  2. Oracle primary,unique,foreign 区别,Hibernate 关联映射

    Oracle primary,unique,foreign 区别 转:http://www.cnblogs.com/henw/archive/2012/08/15/2639510.html NOT N ...

  3. 第六章 Hibernate关联映射

    第六章 hibernate关联映射一.本章知识点分为2部分:1.关联关系:单向多对一关联关系,双向一对多关联关系(含一对多关联关系),多对多关联关系2.延迟加载:类级别加载策略,一对多加载策略,多对一 ...

  4. 【学习笔记】Hibernate关联映射(Y2-1-6)

    Hibernate关联映射 关联映射就是将关联关系映射到数据库里,在对象模型中就是一个或多个引用. 1.单向多对一关联 准备数据库 部门表和员工表 其中部门表有两列 部门编号和名称 员工表有三列 员工 ...

  5. 第三章Hibernate关联映射

    第三章Hibernate关联映射 一.关联关系 类与类之间最普通的关系就是关联关系,而且关联是有方向的. 以部门和员工为列,一个部门下有多个员工,而一个员工只能属于一个部门,从员工到部门就是多对一关联 ...

  6. (转)Hibernate关联映射——对象的三种关系

    http://blog.csdn.net/yerenyuan_pku/article/details/70148618 Hibernate关联映射——对象的三种关系 Hibernate框架基于ORM设 ...

  7. (转)Hibernate关联映射——一对多(多对一)

    http://blog.csdn.net/yerenyuan_pku/article/details/70152173 Hibernate关联映射——一对多(多对一) 我们以客户(Customer)与 ...

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

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

  9. Java三大框架之——Hibernate关联映射与级联操作

    什么是Hibernate中的关联映射? 简单来说Hibernate是ORM映射的持久层框架,全称是(Object Relational Mapping),即对象关系映射. 它将数据库中的表映射成对应的 ...

  10. Hibernate关联映射 映射文件的配置

    一:多对一单向关联 首先我们必须创建两个实体类 例如:Dept类 public class Dept { private Integer deptNo; private String dName; p ...

随机推荐

  1. DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践(2)

    上一篇:<DDD 领域驱动设计-谈谈 Repository.IUnitOfWork 和 IDbContext 的实践(1)> 阅读目录: 抽离 IRepository 并改造 Reposi ...

  2. linq to js使用汇总

    用途:方便js操作查询json数据. 下载网址:http://jslinq.codeplex.com/ 使用方法:只需要引用linq.js即可. 查询方法: 一.where查询 var myList ...

  3. 【分布式】Zookeeper数据与存储

    一.前言 前面分析了Zookeeper对请求的处理,本篇博文接着分析Zookeeper中如何对底层数据进行存储,数据存储被分为内存数据存储于磁盘数据存储. 二.数据与存储 2.1 内存数据 Zooke ...

  4. Scrapy:为spider指定pipeline

    当一个Scrapy项目中有多个spider去爬取多个网站时,往往需要多个pipeline,这时就需要为每个spider指定其对应的pipeline. [通过程序来运行spider],可以通过修改配置s ...

  5. android手机旋转屏幕时让GridView的列数与列宽度自适应

    无意中打开了一年前做过的一个android应用的代码,看到里面实现的一个小功能点(如题),现写篇文章做个笔记.当时面临的问题是,在旋转屏幕的时候需要让gridview的列数与宽度能自适应屏幕宽度,每个 ...

  6. MVC 传值

    1.ViewBag    Controller:ViewBag.Message = "Hello, Word";    View:@ViewBag.Message   注:View ...

  7. C# 之 EXCEL导入导出

    以下方式是本人总结的一些经验,肯定有很多种方法,在此先记下,留待以后补充... 希望朋友们一起来探讨相关想法,请在下方留言. A-1:EXCEL模板导出 非常简单,将EXCEL模板上传到项目中后,将其 ...

  8. Web项目从Oracle转为Mysql,fluentnhibernate-1.0和NHibernate2.1.0升级到NHibernate3.3的注意事项

    1.Oracel数据库没有字段自增长属性,要实现自增长通常是通过查询序列或者触发器来实现的. 设置自增长主键 alter table SUB_SUBSCRIPTION add primary key( ...

  9. Idea SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  10. css3+jquery制作3d旋转相册

    首先来看一下今天的炫酷效果: 首先分析一下这张图片: 1.每张图片都有倒影 2.这11张图片呈圆形均匀排列 3.可旋转,上下移动(当然这是效果做出来以后,图片是分析不出来的) 那下面就开始吧. 一.准 ...