Getting started with hibernate: Do not use hibernate. cfg. xml

Source: Internet
Author: User

Generally, we will create a hibernate. cfg. xml file. This is undoubtedly good. Although the method described below is not good, it is undoubtedly a method;

We can directlyCodeSet a series of parameters;

Main functions:

(1) configuration Config = new configuration ();
// Create a configuration

(2) config.Setproperties(Properties P );
// Import the configuration

(3) config.Addclass(Class C );
// Create a ing (only the class object needs to be specified and the ing file is automatically searched)

Note: When you configure parameters in the code, Hibernate must be added before the parameters, such as Hibernate. Connection. username, which cannot be written as connection. username. If no parameter is added, an error is returned.

 package Org. xiazdong; import Java. util. properties; import Org. hibernate. sessionfactory; import Org. hibernate. transaction; import Org. hibernate. cfg. configuration; import Org. hibernate. classic. session; public class usertest {public static void main (string [] ARGs) {properties P = new properties (); p. put ("hibernate. connection. driver_class "," com. mySQL. JDBC. driver "); p. put ("hibernate. connection. URL "," JDBC: mysql: // hibernate "); p. put ("hibernate. connection. username "," root "); p. put ("hibernate. connection. password "," 12345 "); p. put ("hibernate. dialect "," org. hibernate. dialect. mysqlinnodbdialect "); p. put ("hibernate. hbm2ddl. auto "," Update "); configuration conf = new configuration (). setproperties (P ). addclass (user. class); sessionfactory Sf = Conf. buildsessionfactory (); Session session = SF. opensession (); transaction Tx = session. begintransaction (); User u = new user (); U. setname ("xiazdong-1"); U. setage (20); Session. save (U); Tx. commit (); Session. close (); SF. close () ;}

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.