hibernate--to object-oriented thinking operation relational database (i)

Source: Internet
Author: User

Hibernate is a lightweight ORM Open source Framework. Hibernate freed the face of the tedious JDBC developers, making it become more object-oriented, the previous connection statements, additions and deletions and other statements have been well encapsulated, Make the business logic and so on more easy to write, so that the migration database is also very easy, that is, according to the different database configuration configuration.cfg.xml can easily change the database; The following is a short explanation of the code snippet:


One: each corresponding entity will have a corresponding mapping file, the role of the mapping file is mainly to maintain the relationship between entities.

Entity class User:

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;}}

Map file:

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

To generate a DLL file for the mapping file:

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);}}


Two: in the Configuration.cfg.xml and configure the mapping file information to Configuration.cfg.xml in

Configuration.cfg.xml configuration file:

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">

Three: Building the Client class Client , add user data to Database

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 ();}}}}


Summary: the above is just a small example, but from the small example we can faintly feel that the package to bring us the convenience.


hibernate--to object-oriented thinking operation relational database (i)

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.