PackageCom.ij34.dao;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.*; @Entity @table (name= "People_inf") Public classPeopleImplementsjava.io.serializable{Private Static Final LongSerialversionuid = 1L; @Id @Column (Name= "people_id") @GeneratedValue (Strategy=generationtype.identity)PrivateInteger ID; PrivateString name; Private intAge ; @ManyToMany (targetentity=address.class)//compared to the other, there is no mappedby= "people"@JoinTable (name= "People_address", Joincolumns[Email protected] (name= "Peopleid", referencedcolumnname= "people_id"), Inversejoincolumns[Email protected] (name= "Addressid", referencedcolumnname= "address_id" ) ) PrivateSet<address> address=NewHashset<>(); PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicSet<address>getaddress () {returnaddress; } Public voidSetaddress (set<address>address) { This. Address =address; } }
PackageCom.ij34.dao;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.*; @Entity @table (name= "Address_inf") Public classaddress{@Id @Column (name= "address_id") @GeneratedValue (Strategy=generationtype.identity)Private intAddressid; PrivateString message; @ManyToMany (targetentity=people.class) @JoinTable (name= "People_address", Joincolumns[Email protected] (name= "Addressid", Referencedcolumnname= "address_id")//Don't unique=true ., [email protected] (name= "Peopleid", referencedcolumnname= "people_id" ) ) PrivateSet<people> people=NewHashset<>(); Public intGetaddressid () {returnAddressid; } Public voidSetaddressid (intAddressid) { This. Addressid =Addressid; } PublicString getMessage () {returnmessage; } Public voidsetmessage (String message) { This. Message =message; } PublicSet<people>getPeople () {returnpeople; } Public voidSetpeople (set<people>people) { This. People =people; } }
****
PackageCom.ij34.web; Importorg.hibernate.Session; Importorg.hibernate.SessionFactory; Importorg.hibernate.Transaction; ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder; Importorg.hibernate.cfg.Configuration; ImportOrg.hibernate.service.*;Importcom.ij34.dao.Address;Importcom.ij34.dao.People; Public classtest01 { Public Static voidMain (string[] args)throwsException {//Instantiate a configurationConfiguration conf=NewConfiguration (). Configure (); Serviceregistry SR=NewStandardserviceregistrybuilder (). Applysettings (Conf.getproperties ()). build (); //creating a Sessionfactory instance with a configuration instanceSessionfactory sf=conf.buildsessionfactory (SR); //Create sessionSession session=sf.opensession (); //Start TransactionTransaction tx=session.begintransaction (); People Person=Newpeople (); Person.setage (29); //set values for two members of a composite primary keyPeople people=Newpeople (); People.setage (22); People.setname ("Lin Biao"); //persisting people objects (corresponding to inserting a master table record)Session.save (people); Address a=NewAddress (); A.setmessage (Guangzhou); A.getpeople (). Add (people); Session.persist (a); Address A2=NewAddress (); A2.setmessage (Hong Kong); A2.getpeople (). Add (people); Session.persist (A2); People people2=Newpeople (); People2.setage (17); People2.setname ("Little Fang"); People2.getaddress (). Add (A2); Session.save (People2); Tx.commit (); Session.close (); Sf.close (); } }
***************************************************************************
October 17, 2016 1:48:19 am Org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO:HCANN000001:Hibernate Commons Annotations {4.0.4.Final}
October 17, 2016 1:48:19 am org.hibernate.Version logversion
INFO:HHH000412:Hibernate Core {4.3.5.Final}
October 17, 2016 1:48:19 am org.hibernate.cfg.Environment <clinit>
INFO:HHH000206:hibernate.properties not found
October 17, 2016 1:48:19 am org.hibernate.cfg.Environment buildbytecodeprovider
INFO:HHH000021:Bytecode provider Name:javassist
October 17, 2016 1:48:19 am org.hibernate.cfg.Configuration Configure
INFO:HHH000043:Configuring from resource:/hibernate.cfg.xml
October 17, 2016 1:48:19 am org.hibernate.cfg.Configuration getconfigurationinputstream
INFO:HHH000040:Configuration resource:/hibernate.cfg.xml
October 17, 2016 1:48:19 am org.hibernate.cfg.Configuration doconfigure
INFO:HHH000041:Configured Sessionfactory:null
October 17, 2016 1:48:19 am Org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl Configure
WARN:HHH000402:Using Hibernate built-in connection pool (not for production use!)
October 17, 2016 1:48:19 am Org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl Buildcreator
INFO:HHH000401:using driver [Com.mysql.jdbc.Driver] at URL [Jdbc:mysql://localhost:3306/hibernate]
October 17, 2016 1:48:19 am Org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl Buildcreator
INFO:HHH000046:Connection properties: {user=root, password=****}
October 17, 2016 1:48:19 am Org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl Buildcreator
INFO:HHH000006:Autocommit Mode:false
October 17, 2016 1:48:19 am Org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl Configure
INFO:HHH000115:Hibernate Connection Pool size:20 (min=1)
Mon Oct 01:48:19 CST 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 is established by default if explicit Opti On isn ' t set. For compliance with existing applications not using SSL, the Verifyservercertificate property was set to ' false '. You need either to explicitly disable SSL by setting Usessl=false, or set usessl=true and provide truststore for server CE Rtificate verification.
October 17, 2016 1:48:19 am Org.hibernate.dialect.Dialect <init>
INFO:HHH000400:Using Dialect:org.hibernate.dialect.MySQL5InnoDBDialect
October 17, 2016 1:48:20 am org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateservice
INFO:HHH000399:Using default transaction strategy (direct JDBC transactions)
October 17, 2016 1:48:20 am org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO:HHH000397:Using astquerytranslatorfactory
October 17, 2016 1:48:20 am org.hibernate.tool.hbm2ddl.SchemaUpdate Execute
INFO:HHH000228:Running HBM2DDL Schema Update
October 17, 2016 1:48:20 am org.hibernate.tool.hbm2ddl.SchemaUpdate Execute
INFO:HHH000102:Fetching Database Metadata
October 17, 2016 1:48:20 am org.hibernate.tool.hbm2ddl.SchemaUpdate Execute
INFO:HHH000396:Updating schema
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not Found:address_inf
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not found:people_address
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not Found:people_inf
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not Found:address_inf
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not found:people_address
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not Found:people_inf
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not Found:address_inf
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not found:people_address
October 17, 2016 1:48:20 am Org.hibernate.tool.hbm2ddl.DatabaseMetadata gettablemetadata
INFO:HHH000262:Table not Found:people_inf
October 17, 2016 1:48:23 am org.hibernate.tool.hbm2ddl.SchemaUpdate Execute
INFO:HHH000232:Schema Update Complete
Hibernate:insert to People_inf (age, name) values (?,?)
Hibernate:insert into Address_inf (message) values (?)
Hibernate:insert into Address_inf (message) values (?)
Hibernate:insert to 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 (?,?)
October 17, 2016 1:48:23 am Org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO:HHH000030:Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]
Hibernate----n-n--(people and places)