Write code to access the database through the Hibernate API

Source: Internet
Author: User

Private configuration config;//1. Declaring a private Config object class
Private Serviceregistry serviceregistry;//2. Declaring the private Service registration object class
Private Sessionfactory sessionfactory;//3. Declaring a private session factory object class
Private session session;//4. Declaring a private conversation object class
Private Transaction transaction;//5. Declaring a private transaction object class

@Before
public void Beforestudent () {
Creating a Configuration Object
Config = new Configuration (). Configure ();
Create a Service Registration object
Serviceregistry = new Serviceregistrybuilder (). Applysettings (
Config.getproperties ()). Buildserviceregistry ();
To create a session factory object
Sessionfactory = Config.buildsessionfactory (serviceregistry);
Open session
Session = Sessionfactory.opensession ();
Start a transaction
Transaction = Session.begintransaction ();
}

@After
public void Afterstudent () {
Transaction commit
Transaction.commit ();
Session closed
Session.close ();
Session Factory shutdown
Sessionfactory.close ();
}

@Test
public void Teststudenthbmxml () {
Student Student = new Student (1, "yellow", "male");
Session.save (student);
System.out.println ("in Test ...");
}
}

Write code to access the database through the Hibernate API

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.