Hibernate environment Construction and configuration method (Hello World Profile Edition) _java

Source: Internet
Author: User
Tags create database

This paper gives an example of how to build and configure hibernate environment. Share to everyone for your reference, specific as follows:

1. Download Hibernate jar Package: hibernate-release-4.3.5.final, import the necessary jar package, the path is: hibernate-release-4.3.5.final\lib\required.

There are 10 jar packages included.

2. Create a new Java project.

3. Learn to build your own user Library:

(a) The project right key--build path--configure build Path--add library.
(b) Select User-library to create a new library in which to name hibernate.
(c) The jar package (path: hibernate-release-4.3.5.final\lib\required) required to add hibernate to the library, Hello World is enough, and the others are added.

4. JDBC driver that introduces the database. I used the Mysql:mysql-connector-java-5.1.7-bin.jar.

(a) Create a database:

Create DATABASE hibernate;

(b) Switching the database:

Use hibernate;

(c) Creation of student tables:

CREATE TABLE Student (ID int primary key,name varchar (), age int);

5. The establishment of Hibernate configuration file Hibernate.cfg.xml, strongly recommended in Hibernate-release-4.3.5.final\documentation\manual\en-us\html_ Copy in the Help document under single path.

Location: 1.1.4. Hibernate configuration. After content modification:

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/d Td/hibernate-configuration-3.0.dtd ">  

Establish Student class:

public class Student {
  private int id;
  private String name;
  private int age;
 public int getId () {return
  ID;
 }
 public void setId (int id) {
  this.id = ID;
 }
 Public String GetName () {return
  name;
 }
 public void SetName (String name) {
  this.name = name;
 }
 public int getage () {return age
  ;
 }
 public void Setage (int age) {
  this.age = age;
 }
}

To create a Student mapping file: Student.hbm.xml

<?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 ">
 
 

Final Test:

Import org.hibernate.Session;
Import org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Import com.huxing.hibernate.model.Student;
public class Studenttest {public
 static void Main (string[] args) {
  Student a = new Student ();
  A.setid (123);
  A.setage (a);
  A.setname ("Hello hibernate!");
  Configuration cfg = new Configuration ();
  Sessionfactory CF = Cfg.configure (). Buildsessionfactory ();
  Session session = Cf.opensession ();
  Session.begintransaction ();
  Session.save (a);
  Session.gettransaction (). commit ();
  Session.close ();
  Cf.close ();
 }


I hope this article will help you to hibernate framework program design.

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.