Hibernate DAO implementation

Source: Internet
Author: User

1. First define an interface in the DaO layer:

Public   Interface Basedao {
< T > Void Add (t object );
< T > Void Delete (class clazz, serializable ID );
< T > Void Update (T clazz );
< T > T get (Class < T > Clazz, serializable ID );
< T > List < T > Search (Class < T > Clazz );
< T > List < T > Search (Class < T > Clazz, t condition );
}

 

2. implement this interface in the Dao. impl package:

 

Public   Class Basedaosupport implements basedao {

PrivateSession session;

Protected Session getsession (){
Return Hibernatesessionfactory. getsession ();
}
Public   Void Setsession (session ){
This . Session = Session;
}
/* *
* Added
*/
Public   < T >   Void Add (T Object ){
Transaction TS =   Null ;
Try {
TS = Getsession (). begintransaction ();
Getsession (). Save ( Object );
TS. Commit ();
} Catch (Hibernateexception e ){
If ( Null   ! = TS ){
TS. rollback ();
}
E. printstacktrace ();
} Finally {
Hibernatesessionfactory. closesession ();
}

}
/* *
* Delete
*/
Public   < T >   Void Delete (class clazz, serializable ID ){
Transaction TS =   Null ;
Try {
TS = Getsession (). begintransaction ();
Getsession (). Delete ( Get (Clazz, ID ));
TS. Commit ();
} Catch (Hibernateexception e ){
If ( Null   ! = TS ){
TS. rollback ();
}
E. printstacktrace ();
} Finally {
Hibernatesessionfactory. closesession ();
}
}
/* *
* Update
*/
Public   < T >   Void Update (T clazz ){
Transaction TS =   Null ;
Try {
TS = Getsession (). begintransaction ();
Getsession (). Update (clazz );
TS. Commit ();
} Catch (Hibernateexception e ){
If ( Null   ! = TS ){
TS. rollback ();
}
E. printstacktrace ();
} Finally {
Hibernatesessionfactory. closesession ();
}

}
/* *
* Query a single
*/
Public   < T > T Get (Class < T > Clazz, serializable ID ){
Return (T) getsession (). Get (Clazz, ID );
}
/* *
* Query multiple
*/
Public   < T > List < T > Search (Class < T > Clazz ){
List < T > List = Getsession (). createcriteria (clazz). List ();
Return List;
}
/* *
* Query multiple 2
*/
Public   < T > List < T > Search (Class < T > Clazz, t condition ){
List < T > List = Getsession (). createcriteria (clazz). Add (example. Create (condition). List ();
Return List;
}

 

 

Common addition, deletion, query, and modification methods based on Hibernate are available, and the modification method can be further expanded.

 

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.