Simple and easy-to-understand hibernate example

Source: Internet
Author: User

Database File: myproject. SQL

/* Mysql Data transfersource HOST: localhostsource Database: myprojecttarget HOST: localhosttarget Database: myprojectdate: 2009/8/26 17:23:12 */set foreign_key_checks = 0; -- Optimize table structure for users -- -------------------------- drop table if exists 'users'; Create Table 'users' ('id' int (11) not null auto_increment, 'name' varchar (255) collate utf8_unicode_ci default null, 'Password' varchar (255) Collate utf8_unicode_ci default null, 'email 'varchar (255) Collate utf8_unicode_ci default null, primary key ('id ')) engine = InnoDB auto_increment = 39 default charset = utf8 collate = utf8_unicode_ci; -- export records -- ---------------------------- insert into 'users' values ('34', 'test 4', '100 ', 'OK @ ujs.edu.cn'); insert into 'users' values ('35', 'test 5', '123', 'OK @ ujs.edu.cn '); insert into 'users' values ('36', 'test 6', '000000', 'OK @ ujs.edu.cn'); insert into 'users' values ('37 ', 'test 7', '123', 'OK @ ujs.edu.cn'); insert into 'users' values ('38', 'test 8', '123 ', 'OK @ ujs.edu.cn ');

 

Entity class: user. javapackage usermap; public class user {private int ID; private string name; private string password; private string email; Public String getemail () {return email;} public void setemail (string email) {This. email = Email;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String GetPassword () {return password;} public void setpasswo Rd (string password) {This. password = password;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} hibernate class: hibernatebase. javapackage usermap; import Org. hibernate. *; import Org. hibernate. cfg. *; import Java. util. *; import Java. io. ioexception; import Java. io. printwriter; public abstract class hibernatebase {protected sessionfactory; // session factory, used to create session protected Session session; // hibernate session protected transaction; // hiberante transaction public hibernatebase () throws hibernateexception {This. inithibernate () ;}// help method protected void inithibernate () throws hibernateexception {// load the configuration and construct the sessionfactory object sessionfactory = new configuration (). configure (). buildsessionfactory ();}/*** start a hibernate transaction */protected void begintransaction () throws hibernateex Ception {session = sessionfactory. opensession (); transaction = session. begintransaction ();}/*** ends a hibernate transaction. */Protected void endtransaction (Boolean commit) throws hibernateexception {If (COMMIT) {transaction. Commit ();} else {// if it is a read-only operation, you do not need to commit this transaction. Transaction. rollback ();} session. close () ;}} Bean class: userbean. java package usermap; import Org. hibernate. *; import Org. hibernate. cfg. *; import Java. util. *;/*** business logic related to course */public class userbean extends hibernatebase {public userbean () throws hibernateexception {super ();} /*** Add a course */Public void adduser (User user) throws hibernateexception {begintransaction (); Session. save (User ); Endtransaction (true);}/*** all course in the query system. The returned iterator contains a persistent course object. */Public iterator getallusers () throws hibernateexception {string querystring = "from user U order by u DESC"; begintransaction (); query = session. createquery (querystring); iterator it = query. iterate (); return it;}/*** Delete course */Public void deleteuser (string ID) throws hibernateexception {begintransaction (); User user = (User) session. load (user. class, ID); Session. delete (User ); Endtransaction (true);}/*** perform fuzzy search based on the Course name. The returned iterator contains the course Persistent Object. */Public iterator getsomeuser (string name) throws hibernateexception {string querystring = "select u from user as u where u. name like: Name "; begintransaction (); query = session. createquery (querystring); query. setstring ("name", "%" + name + "%"); iterator it = query. iterate (); return it ;}} ing of user: user. HBM. XML <? XML version = "1.0" encoding = 'utf-8'?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 


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.