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, solve what problem?

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

Get: 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: Version hibernate-distribution-3.6.0.final

(2) Introduction of JAR file: Hibernate3.jar core +required must be introduced (6) +jsp directory +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 do not know can go to hibernate source/project/etc/hibernate.properties find copy paste can

(2) Other configurations include dialect and configuration information that is not automatically constructed

(3) Introduction of the mapping file. <mapping resource= "Directory of 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 that is connected to a 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 profile configuration config=new configuration ();//2. Load configuration file Config.configure ();// The Hiberate.xml file//3 is loaded by default in the SRC directory. Create session factory object Sessionfactory factory = Config.buildsessionfactory ();//4. Create Session ( Represents a conversation, a session with a 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 ();}}
Running results can be found in the database with an employee table, and a record






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate Getting Started case

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.