The first example of hibernate

Source: Internet
Author: User

Let's start with a structure like this.

Includes entity classes, implementation classes , Large configurations , small Configurations ( mapping Files ), and rack packages

Entity class We're not going to focus on this.

Let's start by importing the rack packages we need to get in.

we will first create a new folder named Lib in the src root directory.

Bring in the rack, and then put all the rack bags Bulid path below

The meaning of each rack package represents

Then create the Hibernate profile ( Note that it will be in the src root directory )

Create a mapping file ( under the same package as the entity class )

Code presentation

Entity class

Combined with the above run out correctly shown as:

Extended

Query data public static void Select () {//Build Config object Configuration config=new configuration (). Configure ("Hibernate.cfg.xml")  ;  Build Session Factory Sessionfactory Factory=config.buildsessionfactory ();  Session session=factory.opensession () was created through session factory.  Create transaction Object Transaction tx=session.begintransaction ();  Newsdetail news = (newsdetail) session.load (newsdetail.class,new Integer (5));  System.out.println (News.getntitle ());  Tx.commit ();  Session.close (); Modify the private static void modify () {//Build Config object Configuration config=new configuration (). Configure ("Hibernate.cfg  . xml ");  Build Session Factory Sessionfactory Factory=config.buildsessionfactory ();  Session session=factory.opensession () was created through session factory. Create transaction Object Transaction tx=session.begintransaction ();  Newsdetail news = (newsdetail) session.load (newsdetail.class,new Integer (6));  Newsdetail news=new newsdetail ("hibernate44", "Happy", "December 22, 2015", "Hibernate content Test", 4);  Session.update (news);  Tx.commit (); System.out.prIntln (News.getntitle ());  Session.close ();  Delete the private static void Del () {//Build Config object Configuration config=new configuration (). Configure ("Hibernate.cfg.xml");  Build Session Factory Sessionfactory Factory=config.buildsessionfactory ();  Session session=factory.opensession () was created through session factory.  Create transaction Object Transaction tx=session.begintransaction ();  Newsdetail news = (newsdetail) session.load (newsdetail.class,new Integer (5));  Session.delete (news);  Tx.commit ();  System.out.println (News.getntitle ());   Session.close (); }

  

The first example of hibernate

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.