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

Posted Stay Hungry, Stay Foolish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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--(人与地点)的主要内容,如果未能解决你的问题,请参考以下文章

CSDN写作表情emoji大全

给班上同学演示了一遍用Python选课,结果都要我给他们选课了!

人与狗英文怎么说

如何用 python 做一个考勤系统

黑手党骗局

人与车碰撞的速度模型推导