hibernate----N-1(一)
*************************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(一)的更多相关文章
- hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用
问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...
- hibernate多对一双向关联
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Hibernate中事务的隔离级别设置
Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下
- Hibernate中事务声明
Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...
- spring applicationContext.xml和hibernate.cfg.xml设置
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- [原创]关于Hibernate中的级联操作以及懒加载
Hibernate: 级联操作 一.简单的介绍 cascade和inverse (Employee – Department) Casade用来说明当对主对象进行某种操作时是否对其关联的从对象也作类似 ...
- hibernate的基本xml文件配置
需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...
- Maven搭建SpringMVC+Hibernate项目详解 【转】
前言 今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这 ...
- 1.Hibernate简介
1.框架简介: 定义:基于java语言开发的一套ORM框架: 优点:a.方便开发; b.大大减少代码量; c.性能稍高(不能与数据库高手相比,较一般数据库使用者 ...
随机推荐
- git 仓库、分支的区别
首先,要明白仓库的概念 仓库可以理解为repository, 就是存放代码的地方,—— 其实是一个比较笼统的概念,不管里面的内容,总之存放各种资源(代码.图片等等) 对于git应用开发者而已,本地一般 ...
- Objective-C 随机数
有个项目要给客户发送随机验证码, 试了下这样可以 srand(time()); code = [NSString stringWithFormat: - )) + ];
- Node.js与Sails~项目结构与Mvc实现
回到目录 Sails是一个Node.js的中间件架构,帮助我们很方便的构建WEB应用程序,网址:http://www.sailsjs.org/,它主要是在Express框架的基础上发展起来的,扩展了新 ...
- PHP数据库操作:使用ORM
之前我发了一篇博文PHP数据库操作:从MySQL原生API到PDO,向大家展示PHP是如何使用MySQL原生API.MySQLi面向过程.MySQLi面向对象.PDO操作MySQL数据库的.本文介绍如 ...
- Linux初学 - Elasticsearch环境安装
下载 https://www.elastic.co/downloads/elasticsearch 安装 rpm -ivh 也可以双击rpm包安装 修改elastaticsearch host配置 修 ...
- unity生成的WP8.1工程的Title本地化实现
不同于WP本身工程的实现,unity的工程在package.appxmanifest文件中已经定义了 <Resources> <Resource Language="x-g ...
- 快速入门系列--WCF--02消息、会话与服务寄宿
经过WCF基础的ABC学习,已经可以构建简单的WCF的服务,使用不同的服务地址和绑定类型,根据业务提供所需的服务契约.但不禁想问,服务所使用的消息报文是什么样的形式么?蕴含什么样内容呢?WCF服务是否 ...
- springMVC robots.txt 处理
正常情况这样就好使 <mvc:resources mapping="/robots.txt" location="/lib/robots.txt"/> ...
- 做小图标还用sprite图?你out了!史上最简单易懂iconfont使用教程
1.什么是iconfont? 说白了就是用图标制作而成的一套字体文件,本质是一个字体文件(扩展名是ttf\woff\svg的文件).它是用来制作网页常用小图标的一种方法.以下是天猫首页使用iconfo ...
- 汇编程序的Hello world
原文链接:http://www.orlion.ga/989/ 一.汇编程序的Hello world x86 AT&T: .data msg: .ascii "Hell ...