Hibernate ing mechanism

Source: Internet
Author: User
ObjectRelationMapping (ORM) is a technology designed to solve the mismatch between object-oriented and relational databases. In short, ORM uses

Object Relation Mapping (ORM) is a technology designed to solve the mismatch between object-oriented and relational databases. In short, ORM uses

Hibernate ing mechanism

Object Relation Mapping (ORM) is a technology designed to solve the mismatch between objects and relational databases. In short, ORM uses metadata that describes the ing between objects, automatically persists objects in java programs to relational databases. In essence, this ing mechanism converts data from one form to another.


Basic ing Data Types of Hibernate
The basic ing data type of Hibernate is a bridge between the basic java type and the standard SQL type.
Java type -----------> Hibernate ing data type -----------> standard SQL type
Using the basic ing data type of Hibernate, you can easily convert data from one form to another to complete high-quality ORM tasks.
...

Column = "password"
Type = "string"
...
>


Datamap data table
Bytes -----------------------------------------------------------------------------------------------
Field name data type primary key auto-increment allow blank description
ID int (4) is increased by 1 No idnumber
MYBOOLEAN bit (1) Logical Data
MYINT int (5) Integer Data
MYLONG bigint (11) Long Integer Data
MYFLOAT float (8, 2) Single-precision floating point data
MYDOUBLE double () double Precision Floating Point Data
MYDECIMAL decimal () Decimal data
MySQL string varchar (100) string data
MYTEXT text Text data
MYDATE date Date type data
MYTIME time data
MYDATETIME datetimeDatetime type data
MYTIMESTAMP timestampTimestamp data
MYBINARY varbinary (10240) Binary data
MYBLOB longblobBlob data
Bytes ------------------------------------------------------------------------------------------------
Its corresponding persistence class Datamap. java
Com. hephec. orm
Import java. io. Serializable


Public class Datamap implements Serializable {
Private int hashValue = 0;
Private Integer id;
Private Boolean myboolean;
Private Integer myint;
Private Long mylong;
Private Float myfloat;
Private Double mydouble;
Private BigDecimal mydecimal;
Private String mytext;
Private String mytext;
Private Date mydatel;
Private Time mytime;
Private Date mydatetime;
Private Timestamp mytimestamp;
Private byte [] mybinary;
Private Blob myblob;
Private Datamap () {}// Constructor
// Getter... setter omitted
}
The ORM ing file datamap. hbm. xml of the Datamap table and Datamap class





















(1) Create the data access DAO interface TestDAO. java
Package com. DAO;
Import com. ORM .*;
Public interface TestDAO {
Public void addDatamap (Datamap datamap );
Public Datamap loadDatamap (Integer id );
Public void delDatamap (Integer id );
}


(2) create a data access DAO interface to implement TestDAOImpl. java
Package com. DAO;
Import com. ORM .*;
Import org. hibernate .*;
Public class TestDAOImpl implements TestDAO {
Public void addDatamap (Datamap datamap ){
Session session = MySessionFactory. currentSession ();
Transaction ts = null;
Try {
Ts = session. beginTransaction ();
Session. save (datamap );
Ts. commit ();
} Catch (Exception e ){
If (ts! = Null ){
Ts. rollback ();
}
E. printStackTrace ();
} Finally {
MySessionFactory. closeSession ();
}
}
Public Datamap loadDatamap (Integer id ){


Session session = MySessionFactory. currentSession ();
Transaction ts = null;
Try {
Ts = session. beginTransaction ();
Datamap = (Datamap) session. get (Datamap. class, id );
Ts. commit ();
} Catch (Exception e ){
If (ts! = Null ){
Ts. rollback ();
}
E. printStackTrace ();
} Finally {
MySessionFactory. closeSession ();
}
Return datamap;
}
Public void delDatamap (Integer id ){
Session session = MySessionFactory. currentSession ();
Transaction ts = null;
Try {
Ts = session. beginTransaction ();
Datamap datamap = (Datamap) session. load (Datamap. class, id );
Session. delete (datamap );
Ts. commit ();
} Catch (Exception e ){
If (ts! = Null ){
Ts. rollback ();
}
E. printStackTrace ();
} Finally {
MySessionFactory. closeSession ();
}
}
}
(3) create a TestBean. java for testing
Package com. bean;
Import com. ORM .*;
Import java. io .*;
Import java. math. BigDecimal;
Import java.net .*;
Import org. hibernate .*;

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.