您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何利用Netbeans5.5生成功能來開發Hibernate3”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何利用Netbeans5.5生成功能來開發Hibernate3”吧!
步驟如下。
需要安裝Netbeans5.5或以上版本。
1.使用Netbeans5.5生成EntityClass
2.給EntityClass的id字段注明生成方式,如:@GeneratedValue
3.使用AnnotationConfiguration
注)推薦proxool-0.9.0RC3
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Environment; import com.hb.pack_01.model.P01_Customer; public class BusinessService ...{ public static SessionFactory sessionFactory; static ...{ try ...{ AnnotationConfiguration cfg = new AnnotationConfiguration(); cfg.configure("hibernate.cfg.xml"); cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop"); cfg.addPackage("com.hb.pack_01.model"); cfg.addAnnotatedClass(P01_Customer.class ); sessionFactory = cfg.buildSessionFactory(); } catch (Exception e) ...{ e.printStackTrace(); } } public void saveCustomer(P01_Customer customer) throws Exception ...{ Session session = sessionFactory.openSession(); Transaction tx = null; try ...{ tx = session.beginTransaction(); session.save(customer); tx.commit(); } catch (Exception e) ...{ if (tx != null) ...{ tx.rollback(); } throw e; } finally ...{ session.close(); } } public void test() throws Exception ...{ P01_Customer customer = new P01_Customer(); customer.setName("Laosan Zhang"); customer.setSex(''M''); customer.setCustomerDescription("A good citizen!"); saveCustomer(customer); } public static void main(String[] args) throws Exception ...{ new BusinessService().test(); sessionFactory.close(); } }
Hibernate配置文件:
<?xml version=''1.0'' encoding=''utf-8''?>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
property>
<property name="hibernate.proxool.pool_alias">MSSQL2000POOLproperty>
<property name="hibernate.proxool.xml">Proxool.xmlproperty>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
property>
<property name="show_sql">falseproperty>
<property name="hbm2ddl.auto">createproperty>
session-factory>
hibernate-configuration>
連接池配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>MSSQL2000POOLalias>
<driver-url>jdbc:jtds:sqlserver://localhost:1433/hibernate3driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driverdriver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="sa" />
driver-properties>
<maximum-connection-count>10maximum-connection-count>
<house-keeping-test-sql>
select CURRENT_DATE
house-keeping-test-sql>
proxool>
something-else-entirely>
感謝各位的閱讀,以上就是“如何利用Netbeans5.5生成功能來開發Hibernate3”的內容了,經過本文的學習后,相信大家對如何利用Netbeans5.5生成功能來開發Hibernate3這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。