hibernate----N-N--(人与地点)
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 @Column(name="people_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private int age;
@ManyToMany(targetEntity=Address.class) //与另一个相比,没有mappedBy="people"
@JoinTable(name="people_address"
,joinColumns=@JoinColumn(name="peopleId" ,referencedColumnName="people_id")
,inverseJoinColumns=@JoinColumn(name="addressId" ,referencedColumnName="address_id" )
)
private Set<Address> address=new HashSet<>();
public Integer getId() {
return id;
}
public void setId(Integer 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 Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
} }
package com.ij34.dao; import java.util.HashSet;
import java.util.Set; 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;
@ManyToMany(targetEntity=People.class) @JoinTable(name="people_address"
,joinColumns=@JoinColumn(name="addressId" ,referencedColumnName="address_id" )//不要unique=true
,inverseJoinColumns=@JoinColumn(name="peopleId" ,referencedColumnName="people_id" )
)
private Set<People> people=new HashSet<>();
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 Set<People> getPeople() {
return people;
}
public void setPeople(Set<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.setName("林彪");
// 持久化People对象(对应于插入主表记录)
session.save(people);
Address a=new Address();
a.setMessage("广州");
a.getPeople().add(people);
session.persist(a);
Address a2=new Address();
a2.setMessage("香港");
a2.getPeople().add(people);
session.persist(a2); People people2=new People();
people2.setAge(17);
people2.setName("小芳");
people2.getAddress().add(a2);
session.save(people2);
tx.commit();
session.close();
SF.close();
}
}
***************************************************************************
十月 17, 2016 1:48:19 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
十月 17, 2016 1:48:19 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate]
十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Mon Oct 17 01:48:19 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
十月 17, 2016 1:48:19 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
十月 17, 2016 1:48:20 上午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
十月 17, 2016 1:48:20 上午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: Address_inf
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_address
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_inf
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: Address_inf
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_address
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_inf
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: Address_inf
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_address
十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_inf
十月 17, 2016 1:48:23 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: insert into people_inf (age, name) values (?, ?)
Hibernate: insert into Address_inf (message) values (?)
Hibernate: insert into Address_inf (message) values (?)
Hibernate: insert into people_inf (age, name) values (?, ?)
Hibernate: insert into people_address (addressId, peopleId) values (?, ?)
Hibernate: insert into people_address (addressId, peopleId) values (?, ?)
Hibernate: insert into people_address (peopleId, addressId) values (?, ?)
十月 17, 2016 1:48:23 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]
hibernate----N-N--(人与地点)的更多相关文章
- DataGrid 查出一个列 按要求显示格式 例如:操作人(地点)
这是转换DataGrid显示格式之后 连接字符串的方法 显示:操作人(地点) public static ObservableCollection<CListModel> AllUserL ...
- 迷茫于Hibernate/JPA的人提一些建议。
想对那些“迷惑”于Java ORM框架的J2EE开发人员提一些建议,希望能够对他们 更深入的理解和运用J2EE ORM框架来提速工作有所帮助,这些建议可能显得有些”陈旧“和”肤浅“, 因为最近半年我没 ...
- 1.Hibernate框架
1.分层体系结构与持久化 三层体系结构: 分层体系结构: 指的是将系统的组件分隔到不同的层中,每一层中的组件应保持内聚性,并且应大致在同一抽象级 别: 每一层都应与它下面的各层保持 ...
- (转)Hibernate框架基础——Java对象持久化概述
http://blog.csdn.net/yerenyuan_pku/article/details/52732990 Java对象持久化概述 应用程序的分层体系结构 基于B/S的典型三层架构 说明 ...
- 从JDBC到hibernate再到mybatis之路
一.传统的JDBC编程 在java开发中,以前都是通过JDBC(Java Data Base Connectivity)与数据库打交道的,至少在ORM(Object Relational Mappin ...
- Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加
Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...
- iBatis简单介绍
1. Ibatis是开源软件组织Apache推出的一种轻量级的对象关系映射(ORM)框架,和Hibernate.Toplink等在java编程的对象持久化方面深受开发人员欢迎. 对象关系映 ...
- program testy data
做项目得用数据吧,拿去.... 1.Data.gov搜索 美国政府去年承诺使所有政府数据都能在网上免费获得.这个网站是第一阶段,作为一个门户网站,囊括了从气候到犯罪的一切惊人的信息. 2. 美 ...
- 斜率DP题目
uva 12524 题意:沿河有n个点,每个点有w的东西,有一艘船从起点出发,沿途可以装运东西和卸载东西,船的容量无限,每次把wi的东西从x运到y的花费为(y-x)*wi; 问把n个点的东西合并成k个 ...
随机推荐
- java 内存观察
总结一下上周的工作. 主要就是用到了 jmap jvisualvm jmap 用来生成jvm堆内存的bin文件 jvisualvm则更强大. 待续.
- Infinispan 8 中新的 Redis 缓存存储实现
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/147.html nfinispan 8 包含了一个新的在 Redis k/ ...
- SSM 三大框架整合
上一篇已经讲了整个各个子模块的创建过程以及它们之间的依存关系, 那么这一篇就来正式的整合三大框架(SSM)了. 1, 准备环境1.1 为每个War包工程创建一个Server 那么 添加了Server后 ...
- JS操作页面
DOM操作 1 windows对象操作 属性(值或者子对象): opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null.可以利用这属性来关闭源窗口 方法(函数 ...
- c#设计模式-工厂方法
一. 工厂方法(Factory Method)模式 工厂方法(FactoryMethod)模式是类的创建模式,其用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中. 工厂方法模式是简单工 ...
- chrome远程调试真机上的app
chrome远程调试真机上的app 看来要上真机了...
- Oracle工具之DBNEWID
DBNEWID是Oracle提供的一个用于修改数据库DBID和DBNAME的工具. 在引进该工具之前,如果我们想修改数据库的数据库名,必须重建控制文件.但即便如此,也无法修改该数据库的DBID.众所周 ...
- Lua 学习笔记(四)语句与控制结构
一.赋值与多重赋值 赋值的基本含义是改变一个变量的值或table中字段的值.Lua中允许“多重赋值”,也就是同时为多个值赋予多个变量,每个变量之间以逗号分隔. Lua会先对等号右边 ...
- 邻接表有向图(一)之 C语言详解
本章介绍邻接表有向图.在"图的理论基础"中已经对图进行了理论介绍,这里就不再对图的概念进行重复说明了.和以往一样,本文会先给出C语言的实现:后续再分别给出C++和Java版本的实现 ...
- Chrome 控制台指南
转自:http://blog.jobbole.com/76985/ Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更 ...