利用Netbeans5.5生成功能来开发Hibernate3

开发 后端
本文将介绍如何借助netbeans5.5的EntityClass生成功能开发Hibernate3,EntityClass中的每一个文件对应数据库中的一张表,对应其字段,以及对表的操作的封装,如查询,增加、删除等。

步骤如下。

需要安装Netbeans5.5或以上版本。

1.使用Netbeans5.5生成EntityClass

2.给EntityClass的id字段注明生成方式,如:@GeneratedValue

3.使用AnnotationConfiguration

注)推荐proxool-0.9.0RC3 

  1. import org.hibernate.Session;  
  2. import org.hibernate.SessionFactory;  
  3. import org.hibernate.Transaction;  
  4. import org.hibernate.cfg.AnnotationConfiguration;  
  5. import org.hibernate.cfg.Environment;  
  6.  
  7. import com.hb.pack_01.model.P01_Customer;  
  8.  
  9.  
  10. public class BusinessService ...{  
  11.  
  12.     public static SessionFactory sessionFactory;  
  13.  
  14.     static ...{  
  15.         try ...{  
  16.             AnnotationConfiguration cfg = new AnnotationConfiguration();  
  17.             cfg.configure("hibernate.cfg.xml");  
  18.             cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");  
  19.             cfg.addPackage("com.hb.pack_01.model");  
  20.             cfg.addAnnotatedClass(P01_Customer.class );  
  21.             sessionFactory = cfg.buildSessionFactory();  
  22.         } catch (Exception e) ...{  
  23.             e.printStackTrace();  
  24.         }  
  25.     }  
  26.       
  27.     public void saveCustomer(P01_Customer customer) throws Exception ...{  
  28.         Session session = sessionFactory.openSession();  
  29.         Transaction tx = null;  
  30.         try ...{  
  31.             tx = session.beginTransaction();  
  32.             session.save(customer);  
  33.             tx.commit();  
  34.         } catch (Exception e) ...{  
  35.             if (tx != null) ...{  
  36.                 tx.rollback();  
  37.             }  
  38.             throw e;  
  39.         } finally ...{  
  40.             session.close();  
  41.         }  
  42.     }  
  43.       
  44.     public void test() throws Exception ...{  
  45.  
  46.         P01_Customer customer = new P01_Customer();  
  47.         customer.setName("Laosan Zhang");  
  48.         customer.setSex(''M'');  
  49.         customer.setCustomerDescription("A good citizen!");  
  50.         saveCustomer(customer);  
  51.     }  
  52.       
  53.     public static void main(String[] args) throws Exception ...{  
  54.           
  55.         new BusinessService().test();  
  56.         sessionFactory.close();  
  57.     }  
  58. }  

 Hibernate配置文件:

  1. xml version=''1.0'' encoding=''utf-8''?> 
  2.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  4.  
  5. <hibernate-configuration> 
  6.     <session-factory> 
  7.          
  8.         <property name="hibernate.connection.provider_class"> 
  9.             org.hibernate.connection.ProxoolConnectionProvider  
  10.         property> 
  11.         <property name="hibernate.proxool.pool_alias">MSSQL2000POOLproperty> 
  12.         <property name="hibernate.proxool.xml">Proxool.xmlproperty> 
  13.          
  14.         <property name="dialect"> 
  15.             org.hibernate.dialect.SQLServerDialect  
  16.         property> 
  17.          
  18.         <property name="show_sql">falseproperty> 
  19.          
  20.         <property name="hbm2ddl.auto">createproperty> 
  21.  
  22.     session-factory> 
  23. hibernate-configuration> 
  24.  
  25.  连接池配置文件:  
  26.  
  27.  
  28.  
  29. xml version="1.0" encoding="UTF-8"?> 
  30.  
  31. <something-else-entirely> 
  32.     <proxool> 
  33.         <alias>MSSQL2000POOLalias> 
  34.         <driver-url>jdbc:jtds:sqlserver://localhost:1433/hibernate3driver-url> 
  35.         <driver-class>net.sourceforge.jtds.jdbc.Driverdriver-class> 
  36.         <driver-properties> 
  37.             <property name="user" value="sa" /> 
  38.             <property name="password" value="sa" /> 
  39.         driver-properties> 
  40.         <maximum-connection-count>10maximum-connection-count> 
  41.         <house-keeping-test-sql> 
  42.             select CURRENT_DATE  
  43.         house-keeping-test-sql> 
  44.     proxool> 
  45. something-else-entirely> 

【编辑推荐】

  1. NetBeans 6.0模块快速入门教程
  2. Netbeans 6.0发布,支持Ruby、移动开发和集成的剖析器
  3. NetBeans 6.0预览版发布 Sun再引惊呼
  4. NetBeans成为Ruby开发者的新伙伴(3)
  5. 八大技术牛人点评NetBeans 6.5
责任编辑:张燕妮 来源: diybl.com
相关推荐

2009-06-11 11:03:52

netbeans5.5

2009-06-17 09:17:41

Hibernate3

2009-09-25 11:04:32

Hibernate3实

2009-09-24 10:22:38

Hibernate3新

2009-09-27 11:27:33

Hibernate3JBOSS 3.2

2009-09-24 12:05:35

2009-06-17 09:34:31

Hibernate3Hibernate2连接池

2009-06-15 13:46:00

netbeans配置hibernate

2009-09-23 15:12:41

Hibernate视图

2009-07-17 12:44:01

NetBeans开发S

2009-06-11 09:39:33

netbeans 生成Webservice

2009-07-03 14:15:37

NetBeans6.7

2009-06-12 16:00:13

Session方法Hibernate

2010-05-25 14:54:18

2021-05-05 20:32:36

UbuntuUbuntu MATELinux

2010-06-29 14:34:40

NetBeans 6.NetBeans

2009-06-26 10:12:00

Hibernate自动

2009-06-30 16:52:30

DetchedCrit

2009-06-17 08:47:00

Hibernate优化

2009-09-21 17:55:14

Hibernate O
点赞
收藏

51CTO技术栈公众号