程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> hibernate----N-N--(人與地點),hibernaten1問題

hibernate----N-N--(人與地點),hibernaten1問題

編輯:JAVA綜合教程

hibernate----N-N--(人與地點),hibernaten1問題


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]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved