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

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 logVersionINFO: 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 buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml十月 17, 2016 1:48:19 上午 org.hibernate.cfg.Configuration doConfigureINFO: HHH000041: Configured SessionFactory: null十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureWARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: 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 buildCreatorINFO: HHH000046: Connection properties: {user=root, password=****}十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreatorINFO: HHH000006: Autocommit mode: false十月 17, 2016 1:48:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: 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 initiateServiceINFO: 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 executeINFO: HHH000228: Running hbm2ddl schema update十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000102: Fetching database metadata十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000396: Updating schema十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: Address_inf十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: people_address十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: people_inf十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: Address_inf十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: people_address十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: people_inf十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: Address_inf十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: people_address十月 17, 2016 1:48:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadataINFO: HHH000262: Table not found: people_inf十月 17, 2016 1:48:23 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000232: Schema update completeHibernate: 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 stopINFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]