*************************hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property> <!-- Names the annotated entity class -->
<mapping class="com.ij34.dao.Address"/>
<mapping class="com.ij34.dao.People"/>
</session-factory> </hibernate-configuration>

***************************************************************

package com.ij34.dao;

import java.util.HashSet;
import java.util.Set; import javax.persistence.*; @Entity
@Table(name="people_inf")
public class People implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@Id
private String first;
@Id
private String last;
private int age;
// 记录该People实体关联的所有Address实体
@OneToMany(targetEntity=Address.class,mappedBy="people",cascade=CascadeType.ALL)
private Set<Address> address=new HashSet<>();
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
} }

  

**************************************************************************

package com.ij34.dao;

import javax.persistence.*;

@Entity
@Table(name="Address_inf")
public class Address{
@Id @Column(name="address_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int addressId;
private String message;
@ManyToOne(targetEntity=People.class)
// 使用复合主键
@JoinColumns({
@JoinColumn(name="people_first",referencedColumnName="first",nullable=false),
@JoinColumn(name="people_last",referencedColumnName="last",nullable=false)
})
private People people;
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
} }

**********************************************************************************

package com.ij34.web;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.*; import com.ij34.dao.Address;
import com.ij34.dao.People;
public class test01 {
public static void main(String[] args)throws Exception {
//实例化Configuration
Configuration conf=new Configuration().configure();
ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
// 以Configuration实例创建SessionFactory实例
SessionFactory SF=conf.buildSessionFactory(SR);
//create session
Session session=SF.openSession();
//start 事务
Transaction tx=session.beginTransaction();
People person = new People();
person.setAge(29);
// 为复合主键的两个成员设置值
People people=new People();
people.setAge(22);
people.setFirst("姓林");
people.setLast("名彪");
Address a1=new Address();
a1.setMessage("广州");
a1.setPeople(people);
Address a2=new Address();
a2.setMessage("茂名");
a2.setPeople(people);
session.save(people);
session.save(a1);
session.save(a2);
tx.commit();
session.close();
SF.close();
}
}

****************************************************************

hibernate----N-1(一)的更多相关文章

  1. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  2. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  3. hibernate多对一双向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  4. Hibernate中事务的隔离级别设置

    Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下

  5. Hibernate中事务声明

    Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...

  6. spring applicationContext.xml和hibernate.cfg.xml设置

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. [原创]关于Hibernate中的级联操作以及懒加载

    Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...

  8. hibernate的基本xml文件配置

    需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...

  9. Maven搭建SpringMVC+Hibernate项目详解 【转】

    前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...

  10. 1.Hibernate简介

    1.框架简介: 定义:基于java语言开发的一套ORM框架: 优点:a.方便开发;           b.大大减少代码量;           c.性能稍高(不能与数据库高手相比,较一般数据库使用者 ...

随机推荐

  1. PDO预处理

    方法:bool PDOStatement::execute ([ array $input_parameters ] ) 1.PDOStatement::execute不使用参数 01)单个绑定值(P ...

  2. JS中的宽高(基础知识很重要)

    IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.docu ...

  3. [常见问题]在Linux下执行Redis命令不起作用.

    redis 127.0.0.1:6379> 这个后面无论输入什么命令都没有返回 ok 或者其他的信息,一直保持截图的状态: 解决方法:在SecureCRT中设置Options-->Sess ...

  4. Atitit  深入理解命名空间namespace  java c# php js

    Atitit  深入理解命名空间namespace  java c# php js 1.1. Namespace还是package1 1.2. import同时解决了令人头疼的include1 1.3 ...

  5. 设置easyui input默认值

    /*设置input 焦点*/ $(function () { //集体调用 $(".formTextBoxes input").each(function () { $(this) ...

  6. DOM_05之DOM、BOM常用对象

    1.HTML DOM常用对象之Table:①创建:createTHead():createTBody():createTFoot():②删除:deleteTHead():deleteTFoot():③ ...

  7. 【原创】NIO框架入门(三):iOS与MINA2、Netty4的跨平台UDP双向通信实战

    前言 本文将演示一个iOS客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo.服务端将分别用MINA2和Netty4进行实现,而通信时服务端你只需选其一就行了.同 ...

  8. PackageManager使用

    参考:http://www.linuxidc.com/Linux/2012-02/53072.htm Android系统为我们提供了很多服务管理类,包括ActivityManager.PowerMan ...

  9. Js杂谈-DOM

    前言 对jQuery的依赖.导致js的原生方法的淡忘,如果是封装自己的库,那势必要用到js的许多原生方法.从Jquery强大的dom处理开始,我们开始回顾javascript那些古老而坚挺的DOM方法 ...

  10. php操作mongodb中的ISODate格式日期

    mongodb 中数据记录的日期格式为"dateCreated" : ISODate("2011-12-20T07:22:50.836Z")经过翻阅php官网中 ...