Hibernate ing mechanism

Source: Internet
Author: User

Hibernate ing mechanism

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.
...
<! --
Name: This is a String type attribute in the persistence class User. java.
Column: This is a char (20) field in the database table user.
Type: this is the Hibernate ing type.
-->
<Property name = "password"
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
<Hibernate-mapping package = "com. orm">
<Class name = "Datamap" table = "datamap">
<Id name = "id" column = "ID" type = "Integer">
<Generator class = "identity"/>
</Id>
<Property name = "myboolean" column = "MYBOOLEAN" type = "boolean"/>
<Property name = "myint" column = "MYINT" type = "integer"/>
<Property name = "mylong" column = "MYLONG" type = "long"/>
<Property name = "myfloat" column = "MYFLOAT" type = "float"/>
<Property name = "mydouble" column = "MYDOUBLE" type = "double"/>
<Property name = "mydecimal" column = "MYDECIMAL" type = "decimal"/>
<Property name = "mystring" column = "MYSTRING" type = "string"/>
<Property name = "mytext" column = "MYTEXT" type = "string"/>
<Property name = "mydate" column = "MYDATE" type = "date"/>
<Property name = "mytime" column = "MYTIME" type = "time"/>
<Property name = "mydatetime" column = "MYDATETIME" type = "timestamp"/>
<Property name = "mytimestamp" column = "MYTEMESTAMP" type = "timestamp"/>
<Property name = "mybinary" column = "MYBINARY" type = "binary"/>
<Property name = "myblob" column = "MYBLOB" type = "blob"/>
</Class>
<Hibernate-mapping/>


(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 .*;


Public class TestBean {
TestDAO dao = new TestDAOImpl ();
// Obtain the html content of the specified URL
Private String getHtmlByUrl (String url ){
StringBuffer hmtl = new StringBuffer ();
String line = null;
Try {
URL u = new URL (url );
URLConnection uc = u. openConnection ();
BufferedReader br = new BufferedReader (new InputStreamReader (uc. getInputStream ()));
While (line = (br. readLine ())! = Null ){
Html. append (line );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return html. toString ();
}
// Read binary streams
Private byte [] readBinary (InputStream in ){
Byte [] binCodes = null;
Try {
BinCodes = new byte [in. available ()];
In. read (binCodes );
In. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return binCodes;
}
}
// Create a BLOB object from the output stream
Private java. SQL. Blob getBlob (InputStream in ){
Java. SQL. Blob blob = null;
Try {
Blob = Hibernate. createBlob (in );
In. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return blob;
}
Hibernate ing mechanism
Object Relation Mapping (ORM) is a technology designed to solve the mismatch between object-oriented and relational databases. In short
By using metadata that describes ing between objects, ORM automatically persists objects in java programs to relational databases. This ing mechanism is essentially
Actually, it is to convert 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.
...
<! --
Name: This is a String type attribute in the persistence class User. java.
Column: This is a char (20) field in the database table user.
Type: this is the Hibernate ing type.
-->
<Property name = "password"
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
<Hibernate-mapping package = "com. orm">
<Class name = "Datamap" table = "datamap">
<Id name = "id" column = "ID" type = "Integer">
<Generator class = "identity"/>
</Id>
<Property name = "myboolean" column = "MYBOOLEAN" type = "boolean"/>
<Property name = "myint" column = "MYINT" type = "integer"/>
<Property name = "mylong" column = "MYLONG" type = "long"/>
<Property name = "myfloat" column = "MYFLOAT" type = "float"/>
<Property name = "mydouble" column = "MYDOUBLE" type = "double"/>
<Property name = "mydecimal" column = "MYDECIMAL" type = "decimal"/>
<Property name = "mystring" column = "MYSTRING" type = "string"/>
<Property name = "mytext" column = "MYTEXT" type = "string"/>
<Property name = "mydate" column = "MYDATE" type = "date"/>
<Property name = "mytime" column = "MYTIME" type = "time"/>
<Property name = "mydatetime" column = "MYDATETIME" type = "timestamp"/>
<Property name = "mytimestamp" column = "MYTEMESTAMP" type = "timestamp"/>
<Property name = "mybinary" column = "MYBINARY" type = "binary"/>
<Property name = "myblob" column = "MYBLOB" type = "blob"/>
</Class>
<Hibernate-mapping/>


(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 .*;


Public class TestBean {
TestDAO dao = new TestDAOImpl ();
// Obtain the html content of the specified URL
Private String getHtmlByUrl (String url ){
StringBuffer hmtl = new StringBuffer ();
String line = null;
Try {
URL u = new URL (url );
URLConnection uc = u. openConnection ();
BufferedReader br = new BufferedReader (new InputStreamReader (uc. getInputStream ()));
While (line = (br. readLine ())! = Null ){
Html. append (line );
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return html. toString ();
}
// Read binary streams
Private byte [] readBinary (InputStream in ){
Byte [] binCodes = null;
Try {
BinCodes = new byte [in. available ()];
In. read (binCodes );
In. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return binCodes;
}
}
// Create a BLOB object from the output stream
Private java. SQL. Blob getBlob (InputStream in ){
Java. SQL. Blob blob = null;
Try {
Blob = Hibernate. createBlob (in );
In. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return blob;
}

Hibernate overall understanding

Hibernate details: click here
Hibernate: click here

This article permanently updates the link address:

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.