- To download the Hibernate development package:
Before this chapter, you need to inherit the Hibernate development plugin to eclipse, please refer to my blog: Hibernate (i): Install hibernate plugin to eclipse environment
Website address: http://hibernate.org/
Download page:
The downloaded version is Hibernate5.2.9 (Hibernate-release-5.2.9.final.zip)
Unzip the downloaded development package.
New Project project in Eclipse, project name: hibernate_01
In the project root directory to create a new Lib folder, Hibernate-release-5.2.9.final.zip extract directory is: D:\Work\Java\hibernate-release-5.2.9.Final, put D:\Work\ All jar packages under java\hibernate-release-5.2.9.final\lib\required are copied to the project's new Lib folder.
Locate the MySQL development package and copy it to the Lib folder.
Select all the jar packages under the Lib folder, right-click Menu-"Build Path->add to build Path."
New Hibernate.cfg.xml File
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hi Bernate.org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factory> < Propertyname= "Hibernate.connection.username">Root</ Property> < Propertyname= "Hibernate.connection.password">123456</ Property> < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Hibernate.connection.url">Jdbc:mysql://localhost/hibernate_01</ Property> < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQL5InnoDBDialect</ Property> < Propertyname= "Hibernate.show_sql">True</ Property> < Propertyname= "Hibernate.format_sql">True</ Property> < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property> < Propertyname= "Hibernate.current_session_context_class">Thread</ Property> <MappingResource= "Com/dx/hibernate5/test/news.hbm.xml" /> <Mappingclass= "Com.dx.hibernate5.test.News" /> </session-factory></hibernate-configuration>
New News.java
Packagecom.dx.hibernate5.test;Importjava.util.Date; Public classNews {PrivateInteger ID; PrivateString author; PrivateString content; PrivateDate CreateDate; PublicNews () {} PublicNews (string author, string content, Date createdate) {Super(); This. Author =author; This. Content =content; This. CreateDate =CreateDate; } PublicNews (Integer ID, string author, string content, Date createdate) {Super(); This. ID =ID; This. Author =author; This. Content =content; This. CreateDate =CreateDate; } PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString Getauthor () {returnauthor; } Public voidSetauthor (String author) { This. Author =author; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; } PublicDate getcreatedate () {returnCreateDate; } Public voidsetcreatedate (Date createdate) { This. CreateDate =CreateDate; } @Override PublicString toString () {return"News [id=" + ID + ", author=" + author + ", content=" + content + ", createdate=" + CreateDate + "]"; }}View Code
New News.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2017-4-14 18:41:14 by Hibernate Tools 3.5.0.Final-->< Hibernate-mapping> <className= "Com.dx.hibernate5.test.News" table= "News" > <id name= "id" type= "java.lang.Integer" > <co Lumn name= "ID"/> <generatorclass= "Native"/> </id> <property name= "author" type= "java.lang.String" > <column nam E= "AUTHOR"/> </property> <property name= "content" type= "java.lang.String" > <co Lumn name= "CONTENT"/> </property> <property name= "CreateDate" type= "Java.util.Date" > <column name= "CreateDate"/> </property> </class>New Helloworld.java
Packagecom.dx.hibernate5.test;Importjava.util.Date;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;ImportOrg.hibernate.boot.Metadata;Importorg.hibernate.boot.MetadataSources;ImportOrg.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;ImportOrg.hibernate.boot.registry.StandardServiceRegistry;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder; Public classHelloword { Public Static voidMain (string[] args) {//1. Create a Sessionfactory object//The configuration is the XML config file object that represents hibernate, and if there are no parameters in the Configure method, the default is Hibernate.cfg.xml. //Configuration conf = new configuration (). Configure (); //service registration, which uses the creator mode to build the registration service based on the configuration fields in the configuration file (this should be a common process for registering services in the Hibernate schema). //serviceregistry serviceregistry = new Standardserviceregistrybuilder ()//. Applysettings (Conf.getproperties ())//. Build (); //using a well-instantiated registration service, instantiate the sessionfactory using the Factory mode in the configuration//sessionfactory SF = conf.buildsessionfactory (serviceregistry); //If you are using a Hibernate4 version, this is completely OK and will not be reported mappingexception when running. //But if you use the Hibernate5 version, you will get an error. So how Hibernate5 should build Sessionfactory, as follows://The configure object is not visible to the V5 version than the V4 version. Standard Service registration objects are built directly using creator modeStandardserviceregistry Standardregistry =Newstandardserviceregistrybuilder (). Configure (). build (); //This object metadata object should play the role of a balm, using the above registered object as an entry to build this objectMetadata Metadata =Newmetadatasources (standardregistry). Getmetadatabuilder (). Applyimplicitnamingstrategy (Im plicitnamingstrategycomponentpathimpl.instance). build (); //Finally, the metadata is used to build the SessionfactorySessionfactory sessionfactory =Metadata.getsessionfactorybuilder (). build (); //2. Create a Session objectSession session =sessionfactory.getcurrentsession (); //3. Open thingsTransaction Transaction =session.begintransaction (); //4. Perform save operationNews news =NewNews ("admin", "Hello Hibernate ...",NewDate ()); Session.save (news); //5. Submit thingsTransaction.commit (); //6. Close Session ObjectSession.close (); //7. Close the Sessionfactory objectSessionfactory.close (); System.out.println ("Complete ..."); }}Hibernate (iv): Hello World