程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 第一個成功的Hibernate實例02

第一個成功的Hibernate實例02

編輯:JAVA編程入門知識

  二、在Eclipse中生成POLO和映射文件

  1) 在Eclipse中配置環境,包括數據庫驅動程序(此處略)

  2) 生成POLO類和映射文件

  此處以Houses為例,生成的原代碼和映射文件如下:

  a) hibernate.cfg.xml

  

<?xml version=´1.0´ encoding=´UTF-8´?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;databaseName=RMSDB</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<property name="connection.password">jckjdkmcj</property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<!-- mapping files -->
<mapping resource="zy/pro/hrms/dao/Staffs.hbm.xml"/>
<mapping resource="zy/pro/hrms/dao/Houses.hbm.xml"/>
<mapping resource="zy/pro/hrms/dao/Menus.hbm.xml"/>
<mapping resource="zy/pro/hrms/dao/Tables.hbm.xml"/>
<mapping resource="zy/pro/hrms/dao/Customers.hbm.xml"/>
<mapping resource="zy/pro/hrms/dao/Notice.hbm.xml"/>
</session-factory>
</hibernate-configuration>

  b) Houses.hbm.xml

  

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Fri May 13 14:09:45 CST 2005 -->
<hibernate-mapping package="zy.pro.hrms.dao">
<class name="Houses" table="Houses">
<id name="hid" column="HID" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="alias" column="alias" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
<property name="capity" column="capity" type="java.lang.Integer" not-null="true" />
</class>
</hibernate-mapping>

  c) Houses類

  

/*
* Created Fri May 13 14:09:45 CST 2005 by MyEclipse Hibernate Tool.
*/
package zy.pro.hrms.dao;
import java.io.Serializable;
/**
* A class that represents a row in the ´Houses´ table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Houses
extends AbstractHouses
implements Serializable
{
/**
* Simple constructor of Houses instances.
*/
public Houses()
{
}
/**
* Constructor of Houses instances given a simple primary key.
* @param hid
*/
public Houses(java.lang.Integer hid)
{
super(hid);
}
/* Add customized code below */
}

  3) 將生成的文件導入到JBuilder工程中

  三、編寫測試類

  我們的第一個測試類的代碼如下:

  

package zy.pro.hrms.test;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import zy.pro.hrms.util.HibernateSessionFactory;
import zy.pro.hrms.dao.*;
public class HouseTest {
public HouseTest() {
}
public static void main(String args[]) {
try {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 50; i++) {
Houses h = new Houses();
//h.setHid(new Integer(2));
h.setAlias("dddd");
h.setCapity(new Integer(4));
session.save(h);
tx.commit();
}
//tx.commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

  注意:

  1) 此處的粗體內容,放的位置不同對程序有什麼影響和不同效果(這是我的一個疑點)。

  2) 我們在創建數據庫時不能在數據庫端實現主鍵的自動增長,而應該留給Hibernate來實現主鍵的生成

  --------------------------此程序運行調試成功--------------------------

  運行和編程環境:

  window 2000+Jbuilder X+ SQL Server 2000

  運行環境的配置另附

  還有問題,請和我聯系[email protected]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved