Configure common properties with hibernate.properties instead of Hibernate.cfg.xml

Source: Internet
Author: User

When we use hibernate, we often configure the relevant properties of the database connection in the Hibernate.cfg.xml file, whether to display SQL statements, the dialect of the database, and so on, which can also be configured in the. properties file. Now I take the name of this file as: Hibernate.properties.

Idea: Write a Domian object, and this domain object maps to a. hbm.xml file in the database. and a test class (This test class is a piece of data in the update database)

As well as the Hibernate.properties file (this file is placed under SRC below the root directory of classpath)

One: The code for domain person is:

 PackageCom.qls.domain;Importjava.util.Date;/*** Created by Quinlinson on 2017/5/21.*/ Public classPerson {PrivateInteger ID; PrivateString name; PrivateDate entercampusdate;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicDate getentercampusdate () {returnentercampusdate; }     Public voidsetentercampusdate (Date entercampusdate) { This. entercampusdate =entercampusdate; }}

Two: The code for the Person.hbm.xml file is as follows:

<?xml version= "1.0"? ><! DOCTYPE hibernate-Mapping        Public "-//hibernate/hibernate mapping DTD 3.0//en" Package        = " Com.qls.domain ">    <classclass=" native "/></id><property name=" name "/> <property name= "entercampusdate" type= "timestamp"/>        </class>        

Three: The code for the Hibernate.properties file is as follows:

 Do Not add Semicolon;hibernate.show_sql=truehibernate.connection.driver_class=  Oracle.jdbc.OracleDriverhibernate.connection.url=jdbc:oracle:thin: @localhost: 1521: O Rclhibernate.connection.username=Scotthibernate.connection.password=a123456hibernate.dialect =org.hibernate.dialect.Oracle10gDialecthibernate.connection.pool_size=10

Four: Test class

 Packagecom.qls.test;ImportCom.qls.domain.Person;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;ImportOrg.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.cfg.ImprovedNamingStrategy;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Properties;/*** Created by ${Quinlinson} on 2017/5/22.*/ Public classTest12 { Public Static voidMain (string[] args) {Configuration configuration=NewConfiguration (). AddResource ("/com/qls/domain/person.hbm.xml"); InputStream  in= Test12.        class. getClassLoader (). getResourceAsStream ("hibernate.properties");        Properties Properties = new  properties ();         try  {properties.load (in); }  catch  (IOException e) {e.printstacktrace ();        } configuration.setproperties (properties); Sessionfactory sessionfactory=configuration.buildsessionfactory (); Session Session= Sessionfactory.opensession ();//one sessionTransaction tx = Session.begintransaction ();//Open TransactionPerson Person=session.get.class, 24); Person.setname ("Long live the People");        Session.update (person); Tx.commit ();//commits the transaction.     }}

Configure common properties with hibernate.properties instead of Hibernate.cfg.xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.