Hibernate advanced Hibernate setting up development environment + example explanation

Source: Internet
Author: User

This blog will take you through a simple example to learn the Hibernate project.

1. Create a Java project

2. Create user Library and join the dependency package
* Hibernate_home/lib/*.jar
* Hibernate_home/hibernate3.jar

* Added database driver (MySQL driver)

Specific steps refer to my previous blog: "Hibernate advanced" How to add jar packages in MyEclipse

3, provide hibernate.cfg.xml files, complete the basic configuration

<strong><span style= "FONT-SIZE:18PX;" ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibernate. Sourceforge.net/hibernate-configuration-3.0.dtd ">4, establish the entity class User.java

<strong ><span style= "FONT-SIZE:18PX;" ><strong><span style= "FONT-SIZE:18PX;" >package Com.bjpowernode.hibernate;import Java.util.date;public class User {private string id;private string name; private string password;private date createtime;p rivate date expiretime;public String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public Date Getcreatetime () {return createtime;} public void Setcreatetime (Date createtime) {this.createtime = Createtime;} Public Date Getexpiretime () {return expiretime;} public void Setexpiretime (Date expiretime) {this.expiretime = Expiretime;}} </span></strong></span></strong> 
5, provide User.hbm.xml file, complete the mapping of entity class

<strong><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0"? ><! DOCTYPE hibernate-mapping SYSTEM "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >< Hibernate-mapping ><class name= "Com.bjpowernode.hibernate.User" ><id name= "id" ><generator class= " UUID "/></id><property name=" name "/><property name=" password "/><property name=" CreateTime " /><property name= "Expiretime"/></class>

6. Add the User.hbm.xml file to the Hibernate.cfg.xml file

This was added at 3 paces.

7, write the tool class Exoprtdb.java, will HBM generate DDL, namely HBM2DDL

<strong><span style= "FONT-SIZE:18PX;" >package Com.bjpowernode.hibernate;import Org.hibernate.cfg.configuration;import org.hibernate.tool.hbm2ddl.schemaexport;/** * HBM  Generation DDL * @author CXC * */public class Exoprtdb {public Stati c void Main (string[] args) {//read By default hibernate.cfg.xml fileconfiguration cfg=new Configuration (). Configure (); Schemaexport export=new Schemaexport (CFG); Export.create (true, True);}} </span></strong>
at this point, you can generate the user table, run as Java application, view the database, you can generate the user table.



8. Build client-side clients, add user data to MySQL

<strong><span style= "FONT-SIZE:18PX;" >package Com.bjpowernode.hibernate;import Java.text.simpledateformat;import Java.util.Date;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public Class Client {public static void main (string[] args) {//read hibernate.cfg.xml fileconfiguration cfg=new Configuration (). confi Gure ();//sessionfactory thread Safe//build sessionfactorysessionfactory factory=cfg.buildsessionfactory ();// Acquire session session Session=null;try{session=factory.opensession ();//open transactionsession.begintransaction ( ); User User=new User (), user.setname ("n"), User.setpassword ("123");/* User.setcreatetime (New Date ());                                         User.setexpiretime (new Date ()); */date date=new date ();               SimpleDateFormat temp=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");               String Date2=temp.format (date);                            Date Date3=temp.parse (DATE2); User.setcreatetime (DATE3); User.setexpiretime (date3);//save user objectsession.save (user);//commit transactionsession.gettransaction (). Commit ();} catch (Exception e) {e.printstacktrace ();//rollback transactionsession.gettransaction (). rollback ();} Finally{if (Session!=null) {if (Session.isopen ()) {//close sessionsession.close ();}}}} </span></strong>

In the console, enter the INSERT into user (...) The information represents the insertion of data into the database.

view the inserted data through Navicat



In this way, the Hibernate project is complete.

Problems encountered:

At the time of inserting, encountered the problem that the date cannot be plugged in. Think it is a MySQL reference jar package problem, re-reference is not resolved, think of before also encountered similar problems, the format of the date is resolved.



Hibernate advanced Hibernate Build Development environment + Example tutorial

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.