Hibernate Getting Started case

Source: Internet
Author: User

Hibernate Framework Description:

ORM Concept (very important)

The O,object object.      r,relation relationship. m,mapping Mapping

ORM: Object Relational Mapping!

ORM, what's the problem?

Storage: Whether the object's data can be saved directly into the database

Get: Whether you can get an object directly from the database

To achieve the above two points, there must be a mapping

Summary: Relationship between Hibernate and Orm: Hibernate is the implementation of ORM

How to build a hibernate development environment, development steps:

(1) Download source code: Version number hibernate-distribution-3.6.0.final

(2) Introduction of JAR file: Hibernate3.jar core +required must be introduced (6) +jsp folder +mysql driver

(3) Mapping of objects and objects

Employee.java Object

Mapping of Employee.hbm.xml objects (typically we use the hbm.xml end, which is to differentiate the Hibernate mapping file)

(4) Src/hibernate.cfg.xml (the name of this file must be written in this way)

1.employee.java

Package Cn.itcast.hello;import Java.util.date;public class Employee {private int id;private String empname;private Date W orkdate;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getempname () {return empname;} public void Setempname (String empname) {this.empname = EmpName;} Public Date Getworkdate () {return workdate;} public void Setworkdate (Date workdate) {this.workdate = workdate;}}
2.employee.hbm.xml file

Primarily includes primary key mappings and non-primary key mappings

<?

XML version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">

3.hibernate.cfg.xml file

(1) Configuration of the database connection. If you don't understand, you can go to Hibernate source code/project/etc/hibernate.properties find copy and paste

(2) Other configuration contains dialect and is not the configuration information of their own initiative to build the table

(3) The introduction of the mapping file. <mapping resource= "folder for configuration Files"/>

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibern Ate.org/dtd/hibernate-configuration-3.0.dtd ">4. Test class Apptest

To write the steps:

(1). Gets the management class object that loads the configuration file. (2). Load the configuration file. (3). Create a session factory object. (4). Create a session (which represents a conversation.) The session connected to the database). (5). Turn on the transaction. (6). Operation with the database . (7). Commit the transaction. (8). Close the resource.

Package Cn.itcast.hello;import Java.util.date;import Org.hibernate.sessionfactory;import org.hibernate.Transaction ; Import Org.hibernate.cfg.configuration;import Org.hibernate.classic.session;import Org.junit.test;public class apptest {@Testpublic void Test () {Employee emp=new employee (); Emp.setempname ("Li Weikang"); Emp.setworkdate (new Date ());//1. Gets the management class object that loads the configuration file. Configuration config=new configuration ();//2. load config file config.configure ();// The default load is Hiberate.xml file//3 under the SRC folder. Create session Factory object Sessionfactory factory = Config.buildsessionfactory ();//4. Create Session ( Represents a dialogue. Session with database connection) Session session = Factory.opensession ();//5. Open transaction Transaction tx = Session.begintransaction ();//6. Operation with Database Session.save (EMP);//7. Commit Transaction Tx.commit ();//8. Close Resource Session.close ();}}
Execution results can find a table with one employee in the database and a record






Hibernate Getting Started case

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.