Hibernate (2)

Source: Internet
Author: User

Looking at Hibernate in the project and what I wrote in the book, I always felt a little different. In the afternoon, combined with the MyEclipse + 6 + java Chinese development tutorial, corresponding to Hibernate, I finally got a clue about how to use it in the project.

The textbooks are all written through Hibernate ing tables. Two classes are generated, one corresponding to the POJO (plain and old java object) class, which only corresponds to the get and set methods, operate fields in the table, and the other is DAO class, which is used to update, select, and delete tables. In this case, if there are multiple tables, such as dozens of tables, there are more than two classes, and for DAO classes, basically, the functions of each class are the same. Therefore, it is encapsulated in the project. Four classes are used in total: BaseDaoImpl, BaseDaoInf, BaseTransaction, and HibernateSessionFactory. Among them, BaseDaoInf is an interface, and the methods are implemented in BaseDaoImpl. BaseTransaction is the parent class of the business layer, which implements transaction processing and is inherited by other business objects. HibernateSessionFactory is mapped from it. In this way, you can perform operations on each POJO class through BaseDaoImpl. Corresponds to the Hibernate development example function in "MyEclipse + 6 + java Chinese development tutorial. As follows:

Public static void main (String [] args)
{

// POJO class of the corresponding table
Student bean = new Student ();
Bean. setUsername ("Li Si ");
Bean. setPassword ("123456 ");
Bean. setAge (18 );

// DAO operations are encapsulated in BaseDaoImpl
BaseDaoImpl impl = new BaseDaoImpl ();

// Obtain and set the Session in this step
Impl. setSession (HibernateSessionFactory. getSession ());

// Add record
Impl. saveObject (bean );

// Use HQL to query
Java. util. List <Student> results = impl. hqlQuery ("from Student s ");
For (Student o: results)
{
System. out. println ("No.:" + o. getId ());
System. out. println ("name:" + o. getUsername ());
}
}

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.