hibernate----N-N--(人与地点)

时间:2022-12-15 23:58:45
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;
} }

hibernate----N-N--(人与地点)

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;
} }

hibernate----N-N--(人与地点)

****

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();
}
}

hibernate----N-N--(人与地点)

hibernate----N-N--(人与地点)

hibernate----N-N--(人与地点)

hibernate----N-N--(人与地点)

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

十月 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--(人与地点)的更多相关文章

  1. DataGrid 查出一个列 按要求显示格式 例如:操作人(地点)

    这是转换DataGrid显示格式之后 连接字符串的方法 显示:操作人(地点) public static ObservableCollection<CListModel> AllUserL ...

  2. 迷茫于Hibernate&sol;JPA的人提一些建议。

    想对那些“迷惑”于Java ORM框架的J2EE开发人员提一些建议,希望能够对他们 更深入的理解和运用J2EE ORM框架来提速工作有所帮助,这些建议可能显得有些”陈旧“和”肤浅“, 因为最近半年我没 ...

  3. 1&period;Hibernate框架

    1.分层体系结构与持久化 三层体系结构: 分层体系结构: 指的是将系统的组件分隔到不同的层中,每一层中的组件应保持内聚性,并且应大致在同一抽象级           别: 每一层都应与它下面的各层保持 ...

  4. (转)Hibernate框架基础——Java对象持久化概述

    http://blog.csdn.net/yerenyuan_pku/article/details/52732990 Java对象持久化概述 应用程序的分层体系结构 基于B/S的典型三层架构  说明 ...

  5. 从JDBC到hibernate再到mybatis之路

    一.传统的JDBC编程 在java开发中,以前都是通过JDBC(Java Data Base Connectivity)与数据库打交道的,至少在ORM(Object Relational Mappin ...

  6. Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加

    Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...

  7. iBatis简单介绍

    1.       Ibatis是开源软件组织Apache推出的一种轻量级的对象关系映射(ORM)框架,和Hibernate.Toplink等在java编程的对象持久化方面深受开发人员欢迎. 对象关系映 ...

  8. program testy data

    做项目得用数据吧,拿去.... 1.Data.gov搜索   美国*去年承诺使所有*数据都能在网上免费获得.这个网站是第一阶段,作为一个门户网站,囊括了从气候到犯罪的一切惊人的信息.   2. 美 ...

  9. 斜率DP题目

    uva 12524 题意:沿河有n个点,每个点有w的东西,有一艘船从起点出发,沿途可以装运东西和卸载东西,船的容量无限,每次把wi的东西从x运到y的花费为(y-x)*wi; 问把n个点的东西合并成k个 ...

随机推荐

  1. 部署Qt程序时plugins相关问题

    部署qt程序时,经常涉及到Qt5.5.0\5.5\msvc2013\plugins目录下的一些动态链接库 例如数据库sqldrivers,操作系统类型platforms,读取各种图片imageform ...

  2. 块状元素&lpar;div&rpar;与内联元素(span)

    <pre class="html" name="code"><html xmlns="http://www.w3.org/1999/ ...

  3. Linux - gcc和g&plus;&plus;的区别

    一般linux系统都自带了gcc编译器的,你可以用你的安装光盘去安装,如果你是觉得自带的gcc版本太低了,可以去gcc的官方网站可以下载到,编译需要很长的时间,如果你只编译C或者C++可以只下载gcc ...

  4. Android 开发实践 ViewGroup 实现左右滑出窗口(一)

    利用假期把以前做的东西总结整理一下,先从简单的开始吧.实现的效果是这样的:   做了个截屏动画,比例有点不对了,凑合着看吧. 整个窗口有3部分组成,中间的主界面是个列表,左边的滑出界面是个菜单,右边的 ...

  5. mysql 索引相关知识

    由where 1 =1 引发的思考 最近工作上被说了 说代码中不能用 where 1=1,当时觉得是应该可以用的,但是找不到什么理据, 而且mysql 语句优化这方面确实很薄弱   感觉自己mysql ...

  6. 矩形A &plus; B HDU2524

    题意 给你n*m的棋盘问有多少个矩形 分析 先看只有一行或一列的情况有1+2+....+n个,因为矩形的类型有1个最小单位格子n个,2个最小单位格子n-1个,n个最小单位格子有一个 code #inc ...

  7. hdu 3072 Intelligence System&lpar;Tarjan 求连通块间最小值&rpar;

    Intelligence System Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  8. scala private 和 private&lbrack;this&rsqb; 的区别

    class PackageStudy { private var a = 0 private[this] var b = 1 // 只能在类内部使用,对象都不能直接使用 def getb(): Int ...

  9. CSS&lowbar;Spirte实现原理 分类: HTML&plus;CSS 2015-04-28 22&colon;58 531人阅读 评论&lpar;0&rpar; 收藏

    CSS Spirte就是所谓的把很多的小图标合并成一张大的图片,然后使用CSS的background-position属性,来动态的定位自己需要图标的位置.这样做的目的主要是减少HTTP请求,加快网页 ...

  10. 交叉编译 Cross-compiling for Linux

    @(134 - Linux) Part 1 交叉编译简介 1.1 What is cross-compiling? 对于没有做过嵌入式编程的人,可能不太理解交叉编译的概念,那么什么是交叉编译?它有什么 ...