Problem: We often write the following line of code when we get a Sessionfactory object:
1 New Configuration (). Configure (). Buildsessionfactory ();
So what does this line of code do, and what does the object of the configuration do?
To answer these questions, you must first know the role of the configuration object.
The function of the Configuration is: an instance of Org.hibernate.cfg.Configuration represents a entire set of mapping of an application ' s J Ava types to an SQL database.
The org.hibernate.cfg.Configuratio is used to build a immutable org.hibernate.SessionFactory.the mappings is compiled fr Om various XML mapping files.
After knowing the role of the object of the configuration, we can completely not configure the file Hibernate.cfg.xml and configure it inside. You only need to load the properties and mapping files for the domain object in one class.
First look at the directory structure of this project:
Where person is a domain object, Person3.hbm.xml is a configuration file that the person object is associated with the table. Test10 is a test class that tests the data to be updated under a file that is not hibernate.cfg.xml.
The code for the Person class is as follows:
1 PackageCom.qls.domain;2 3 Importjava.util.Date;4 5 /**6 * Created by Quinlinson on 2017/5/21.7 */8 Public classPerson {9 PrivateInteger ID;Ten PrivateString name; One PrivateDate entercampusdate; A - PublicInteger getId () { - returnID; the } - - Public voidsetId (Integer id) { - This. ID =ID; + } - + PublicString GetName () { A returnname; at } - - Public voidsetName (String name) { - This. Name =name; - } - in PublicDate getentercampusdate () { - returnentercampusdate; to } + - Public voidsetentercampusdate (Date entercampusdate) { the This. entercampusdate =entercampusdate; * } $}
The code for the Person3.hbm.xml file is as follows:
1<?xml version= "1.0"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >5 6 Package= "Com.qls.domain" >7<className= "person" table= "person" >8<id name= "id" column= "person_id" >9<generatorclass= "Native"/>Ten</id> One<property name= "Name"/> A<property name= "entercampusdate" type= "timestamp"/> -</class> -The code for the Test10 class is as follows:
1 Packagecom.qls.test;2 3 ImportCom.qls.domain.Person;4 Importorg.hibernate.Session;5 Importorg.hibernate.SessionFactory;6 Importorg.hibernate.Transaction;7 Importorg.hibernate.cfg.Configuration;8 9 /**Ten * Created by ${Quinlinson} on 2017/5/22. One */ A Public classTest10 { - Public Static voidMain (string[] args) { -Configuration Configuration =NewConfiguration (); the //Set database connection. - Configuration -. AddResource ("/com/qls/configurationfile/person3.hbm.xml"//com The front slash cannot be omitted. Must be written in the form of/com. -. SetProperty ("Hibernate.show_sql", "true") +. SetProperty ("Hibernate.connection.driver_class", "Oracle.jdbc.OracleDriver") -. SetProperty ("Hibernate.connection.url", "Jdbc:oracle:thin: @localhost: 1521:orcl") +. SetProperty ("Hibernate.connection.username", "Scott") A. SetProperty ("Hibernate.connection.password", "a123456") at //Set dialect Properties -. SetProperty ("Hibernate.dialect", "Org.hibernate.dialect.Oracle10gDialect") -. SetProperty ("Hibernate.connection.pool_size", "10"); -Sessionfactory sessionfactory =configuration.buildsessionfactory (); -Session session = Sessionfactory.opensession ();//get the conversation. -Transaction tx = Session.begintransaction ();//Open Transaction inPerson p = session.get (person.class, 24); - //update the data. toP.setname ("Sinking Fish"); + session.update (p); - tx.commit (); the } *}
As for the new configuration (). Configure () Open source code can be seen, he is reading SRC under the configuration file Hibernate.cfg.xml.
The role of configuration classes in Hibernate