您好,登录后才能下订单哦!
这篇文章主要讲解了“如何利用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。