Configuring Hibernate in Eclipse

Source: Internet
Author: User
Tags rollback

All jar packages under the hibernate/lib/required directory are imported under the path path. and a jar package to connect to the database


Hierarchical structure


Hibernate.cfg.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-configuration public
        "-//hibernate/hibernate configuration DTD 3.0//en"
        "http:// Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ">
        

User.java

Package org.zbq.util;

Import java.util.List;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.hibernate.service.ServiceRegistryBuilder;

Import Org.zbq.bean.User;
	
	public class Hibernateutil {private static sessionfactory sessionfactory;
			static{try{Configuration conf = new Configuration ();
			Conf.configure (); serviceregistry sr = new Serviceregistrybuilder (). Applysettings (Conf.getproperties ()). Buildserviceregi
			
			Stry ();
		Sessionfactory = Conf.buildsessionfactory (SR);
		catch (Exception e) {e.printstacktrace ();
		} public static void Save (user user) throws exception{Transaction tr = null;
		
		Session session = Sessionfactory.opensession ();
			try{tr = session.begintransaction ();//Tr.begin ();
			Session.save (user);
		Tr.commit (); catch (exceptioN e) {if (null!= TR) {tr.rollback ();
		} throw E;
		finally {session.close (); @SuppressWarnings ("unchecked") public static list<user> Listusers () throws exception{list<user> Li
		st = NULL;
		Transaction tr = null;
		
		Session session = Sessionfactory.opensession ();
			try{tr = session.begintransaction ();//Tr.begin ();
			String sql = "from User";
			
			Query query = session.createquery (SQL);
			List = (list<user>) query.list ();
		Tr.commit ();
		catch (Exception e) {e.printstacktrace ();
		finally {session.close ();
	} return list;
 }
}

User.hbm.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-mapping public  
        "-//hibernate/hibernate mapping DTD 3.0//en"  
        "http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd "> 


Hibernateutil.java

Package org.zbq.util;

Import java.util.List;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.service.ServiceRegistry;
Import Org.hibernate.service.ServiceRegistryBuilder;

Import Org.zbq.bean.User;
	
	public class Hibernateutil {private static sessionfactory sessionfactory;
			static{try{Configuration conf = new Configuration ();
			Conf.configure (); serviceregistry sr = new Serviceregistrybuilder (). Applysettings (Conf.getproperties ()). Buildserviceregi
			
			Stry ();
		Sessionfactory = Conf.buildsessionfactory (SR);
		catch (Exception e) {e.printstacktrace ();
		} public static void Save (user user) throws exception{Transaction tr = null;
		
		Session session = Sessionfactory.opensession ();
			try{tr = session.begintransaction ();//Tr.begin ();
			Session.save (user);
		Tr.commit (); catch (exceptioN e) {if (null!= TR) {tr.rollback ();
		} throw E;
		finally {session.close (); @SuppressWarnings ("unchecked") public static list<user> Listusers () throws exception{list<user> Li
		st = NULL;
		Transaction tr = null;
		
		Session session = Sessionfactory.opensession ();
			try{tr = session.begintransaction ();//Tr.begin ();
			String sql = "from User";
			
			Query query = session.createquery (SQL);
			List = (list<user>) query.list ();
		Tr.commit ();
		catch (Exception e) {e.printstacktrace ();
		finally {session.close ();
	} return list; }
}
Test class

Package org.zbq.test;

Import java.util.List;

Import Org.zbq.bean.User;
Import Org.zbq.util.HibernateUtil;

public class Hibernatutiltest {public

	static void Main (string[] args) throws Exception {
		User user = new user (); C6/>user.setid (2);
		User.setname ("cat");
		User.setpass ("intel123");
		Hibernateutil.save (user);
		
		list<user> list = Hibernateutil.listusers ();
		for (User u:list) {
			System.out.println (u.getname () + ":" + U.getpass ());}}}


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.