Some hibernate operations

Source: Internet
Author: User

Package com. yl. Demo;

Import java. util. iterator;
Import java. util. List;

Import org. hibernate. hibernateexception;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. transaction;
Import org. hibernate. cfg. configuration;

Import com. yl. Bean. classes;
Import com. yl. Bean. student;
Import com. yl. utils. dbutils;

Public class htest {

/**
* @ Param Stu
*/
Public void insert (student Stu ){
// Read the configuration file
Configuration conf = new configuration (). Configure ();
// Obtain the factory
Sessionfactory Sf = Conf. buildsessionfactory ();
// Obtain the session
Session session = NULL;
Transaction Tx = NULL;
Try {
Session = SF. opensession ();
Tx = session. begintransaction ();
Session. Save (Stu );
TX. Commit ();
} Catch (hibernateexception e ){
TX. rollback ();
E. printstacktrace ();
} Finally {
If (session! = NULL & session. isopen ())
Session. Close ();
}

}
Public void findbyid (int id ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student where id =? ");
Query. setinteger (0, ID );
Student Stu = (student) query. uniqueresult ();
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
System. Out. println (STU. getname ());
}
Public void findall01 (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student ");
List <student> List = query. List ();
For (student STU: List ){
System. Out. println (STU. getname ());
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void findall02 (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student ");
Iterator <student> it = query. iterate ();
While (it. hasnext ()){
System. Out. println (it. Next (). getname ());
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void getbyid (int id ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Student Stu = (student) Session. Get (student. Class, ID );
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
System. Out. println (STU. getname ());
}
Public void loadbyid (int id ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Student Stu = (student) Session. Load (student. Class, ID );
// System. Out. println (STU. getname ());
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
System. Out. println (STU. getname ());

}
Public void findsome (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("Select name, age from student ");
List list = query. List ();
For (Object O: List ){
Object [] Ob = (object []) O;
System. Out. println (OB [0] + "" + OB [1]);
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void findlike (){
Session session = dbutils. getsession ();
Session. begintransaction ();
Query query = session. createquery ("from student where name like? ");
Query. setparameter (0, "% C % ");
List <student> List = query. List ();
For (student STU: List ){
System. Out. println (STU. getname ());
}
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void Update (student Stu ){
Session session = dbutils. getsession ();
Session. begintransaction ();
Session. Update (Stu );
Session. gettransaction (). Commit ();
Dbutils. getclose (session );
}
Public void insertcia (Classes CIA ){
// Obtain the session
Session session = dbutils. getsession ();
// Start the transaction
Session. begintransaction ();
Session. Save (FIG );
// Transaction commit
Session. gettransaction (). Commit ();
// Close
Dbutils. getclose (session );

}
Public static void main (string [] ARGs ){
Htest ht = new htest ();
Student Stu = new student ();
Stu. setid (1 );
Stu. setname ("aabbcc ");
Stu. setage (20 );
Classes L4 = new classes ();
Linoleic. setclassroom ("306 ");
Linoleic. setname ("Java ");
Stu. setlinoleic (linoleic );
// Ht. insert (Stu );
// Ht. findbyid (1 );
// Ht. findall01 ();
// Ht. findall02 ();
// Ht. getbyid (1 );
// Ht. loadbyid (1 );
// Ht. findsome ();
// Ht. Update (Stu );
Ht. findlike ();
}
}
 

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.