Getting started with Hibernate4

Source: Internet
Author: User

Getting started with Hibernate4
1. download the latest Hibernate2. install the Hibernate tool plug-in eclipse.

Download the latest luna plug-in
After installation, the new menu should include:

3. Create a New java Project
Add all jar packages in the downloaded/lib/required package in hibernate, and add the drivers connecting to the database.
4. Create a configuration file for hibernate. cfg. xml in the src directory (you can create it using the plug-in)

Write database information and some settings in xml

    
          
      
           
            
    
     
JavaTest
            
    
     
123456
            
    
     
Com. mysql. jdbc. Driver
            
    
     
Jdbc: mysql: // localhost: 3306/sample
            
            
            
    
     
Org. hibernate. dialect. MySQLInnoDBDialect
            
            
    
     
True
            
            
    
     
True
            
            
    
     
Update
            
            
         
    
       
  
5. Create News class, get set, constructor, etc...
package com.qhn;import java.sql.Date;public class News { private Integer id; private String title; private String author; private Date date; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; } public News(String title, String author, Date date) { super(); this.title = title; this.author = author; this.date = date; } public News() { }}
6. Use a plug-in to generate the. hbm. xml file of News.
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated May 14, 2015 8:33:47 PM by Hibernate Tools 3.4.0.CR1 -->
7. Create a test class
Package com. qhn; import static org. junit. assert. *; import java. SQL. date; import org. hibernate. *; import org. hibernate. cfg. configuration; import org. hibernate. service. serviceRegistry; import org. hibernate. service. serviceRegistryBuilder; import org. junit. test; @ SuppressWarnings ("deprecation") public class HibernateTest {@ Test public void test () {// 1. sessionfecw.sessionfactory SessionFactory = null; Configuration configuration = new Configuration (). configure (); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (configuration. getProperties ()). buildServiceRegistry (); sessionFactory = configuration. buildSessionFactory (serviceRegistry); // 2. sesson Session session = sessionFactory. openSession (); // 3. enable Transaction transaction Transaction = session. beginTransaction (); // 4. run the save operation News news = new News ("java", "HI", new Date (new java. util. date (). getTime (); session. save (news); // 5. submit transaction. commit (); // 6. disable Session. close (); // 7. disable sessionfecsponsessionfactory. close ();}}

After running, you can find the newly created table and insert data in the corresponding database.

The file structure of the entire project is as follows:
├── bin│ ├── com│ │ └── qhn│ │ ├── HibernateTest.class│ │ ├── News.class│ │ └── News.hbm.xml│ └── hibernate.cfg.xml├── lib│ ├── antlr-2.7.7.jar│ ├── dom4j-1.6.1.jar│ ├── hibernate-commons-annotations-4.0.5.Final.jar│ ├── hibernate-core-4.3.9.Final.jar│ ├── hibernate-jpa-2.1-api-1.0.0.Final.jar│ ├── jandex-1.1.0.Final.jar│ ├── javassist-3.18.1-GA.jar│ ├── jboss-logging-3.1.3.GA.jar│ ├── jboss-logging-annotations-1.2.0.Beta1.jar│ ├── jboss-transaction-api_1.2_spec-1.0.0.Final.jar│ └── mysql-connector-java-5.1.34-bin.jar└── src ├── com │ └── qhn │ ├── HibernateTest.java │ ├── News.hbm.xml │ └── News.java └── hibernate.cfg.xml

Related Article

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.