A simple example of rookie learning hibernate--

Source: Internet
Author: User

One,Hibernatedevelopment.

The previous blog has introduced the persistence layer framework of the development process, the type of persistence layer framework.

in order to be able to use Hibernate high-speed, we first explain a simple Hibernate Application Examples Hibernate_first .

Second, the development process.

1. First Create a new Hibernate_first project in Myeclipce, and then create the new project folder as:


2. Configure the Hibernate environment.

3. Writing Persistent classes User.java

Package Com.bjpowernode.hibernate;import Java.util.date;public class User {private string id;private string name; private string password;private date createtime;private 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;}}


4. Write the build map file User.hbm.xml.

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">


5. Write the hibernate.cfg.xml file.

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
6. Generate the table's class Exportdb.java.

Package Com.bjpowernode.hibernate;import Org.hibernate.cfg.configuration;import org.hibernate.tool.hbm2ddl.schemaexport;/** * Generate DDL for HBM * @author Administrator * */public class Exportdb {public static V OID Main (string[] args) {//default read hibernate.cfg.xml file configuration cfg = new configuration (). Configure (); Schemaexport export = new Schemaexport (CFG); Export.create (true, True);}}


7. The above six steps have built up the table below and we'll save the data. Create a new Client.java class to deposit one data.

The code is as follows:

Package Com.bjpowernode.hibernate;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 file configuration cfg = new configuration (). Configure ();// Establish Sessionfactorysessionfactory factory = Cfg.buildsessionfactory ();//Get sessionsession session = Null;try {session = Factory.opensession ();//Open transaction session.begintransaction (); User user = new user (), User.setname ("Zhang San"), User.setpassword ("123"); User.setcreatetime (new Date ()); user.setexpiretime (New Date ());//Save User Object Session.save (user);//COMMIT Transaction session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace ();//ROLLBACK TRANSACTION session.gettransaction (). rollback ();} finally {if (session! = NULL) {if (Session.isopen ()) {//close sessionsession.close ();}}}}



Third, summary.

a simple Hibernate The sample is coming out of the Hibernate This is a good example of how we can get started with this sample.



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

A simple example of rookie learning hibernate--

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.