网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  开发框架 hibernate3.0 开发实例     
  文章作者:未知  文章来源:水木森林  
  查看:127次  录入:管理员--2007-11-17  
 
  建议是有一点点是一点点基础的人又没有hibernate基础和经验的人比较适合(所谓一点点基础是最起码不要我介绍一些配置文件的什么的.).注意我用的jdbc驱动com.inet.tds.tdsdriver
  
  大家一起努力学习吧.
  
  建立po对象
  
  建po对象customer、order以及它们的hibernate配置文件
  
  /*
  * created on 2005-10-12
  *
  * todo to change the template for this generated file go to
  * window - preferences - java - code style - code templates
  */
  package testdemo;
  
  import java.util.hashset;
  import java.util.set;
  
  /**
  * @author liuzj
  *
  * todo to change the template for this generated type comment go to window -
  * preferences - java - code style - code templates
  */
  
  public class customer {
  private int id;
  
  private string username;
  
  private string password;
  
  private set orders = new hashset();
  
  public customer() {
  }
  
  public customer(string username, string password, set orders) {
  this.username = username;
  this.password = password;
  
  this.orders = orders;
  
  }
  
  public int getid() {
  return id;
  }
  
  public string getpassword() {
  return password;
  }
  
  public string getusername() {
  return username;
  }
  
  public set getorders() {
  return orders;
  }
  
  public void setid(int id) {
  this.id = id;
  }
  
  public void setpassword(string password) {
  this.password = password;
  }
  
  public void setusername(string username) {
  this.username = username;
  }
  
  public void setorders(set orders) {
  this.orders = orders;
  }
  
  }
  
  <?xml version="1.0"?>
  <!doctype hibernate-mapping public
  "-//hibernate/hibernate mapping dtd 3.0//en"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <hibernate-mapping>
  <class name="testdemo.customer" table="customer" dynamic-insert="true" dynamic-update="true">
  <id name="id" column="id">
  <generator class="increment" />
  </id>
  <property name="username" column="username" />
  <property name="password" column="password" />
  
  <set
  name="orders"
  inverse="true"
  cascade="save-update"
  >
  <key column="customer_id" />
  <one-to-many class="testdemo.order" />
  </set>
  
  </class>
  </hibernate-mapping>
  
  /*
  * created on 2005-10-13
  *
  * todo to change the template for this generated file go to
  * window - preferences - java - code style - code templates
  */
  package testdemo;
  
  /**
  * @author liuzj
  *
  * todo to change the template for this generated type comment go to
  * window - preferences - java - code style - code templates
  */
  
  import java.io.serializable;
  public class order implements serializable {
  
  private long id;
  private string ordernumber;
  private double price;
  private customer customer;
  
  public order() {
  }
  public order(string ordernumber,double price,customer customer) {
  this.ordernumber=ordernumber;
  this.price=price;
  this.customer=customer;
  }
  public long getid() {
  return this.id;
  }
  
  public void setid(long id) {
  this.id = id;
  }
  
  public string getordernumber() {
  return this.ordernumber;
  }
  
  public void setordernumber(string ordernumber) {
  this.ordernumber = ordernumber;
  }
  
  public customer getcustomer() {
  return this.customer;
  }
  
  public void setcustomer(customer customer) {
  this.customer = customer;
  }
  
  public double getprice(){
  return this.price;
  }
  private void setprice( double price ){
  this.price = price;
  }
  }
  
  <?xml version="1.0"?>
  <!doctype hibernate-mapping public
  "-//hibernate/hibernate mapping dtd 3.0//en"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  
  <hibernate-mapping package="testdemo">
  
  <class name="order" table="orders">
  
  <id name="id">
  <generator class="increment"/>
  </id>
  
  <property name="ordernumber" column="order_number"/>
  <property name="price" />
  
  <many-to-one
  name="customer"
  column="customer_id"
  class="customer"
  not-null="true"
  />
  
  </class>
  </hibernate-mapping>
  
  上面的po已经建立完成,下面是一个测试类
  
  /*
  * created on 2005-10-12
  *
  * todo to change the template for this generated file go to
  * window - preferences - java - code style - code templates
  */
  package testdemo;
  
  import java.util.hashset;
  
  import org.hibernate.session;
  import org.hibernate.sessionfactory;
  import org.hibernate.transaction;
  import org.hibernate.cfg.configuration;
  
  import testdemo.customer;
  
  /**
  * @author liuzj
  *
  * todo to change the template for this generated type comment go to window -
  * preferences - java - code style - code templates
  */
  
  public class test {
  
  sessionfactory sessionfactory = new configuration().configure().addclass(
  testdemo.customer.class).addclass(order.class).buildsessionfactory();
  session session = sessionfactory.opensession();
  public void savecustomer(customer customer) throws exception {
  
  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 testmethod()throws exception
  {
  customer customer=new customer("lzhengj","001",new hashset());
  order order1=new order("order",1000,customer);
  order order2=new order("order",2000,customer);
  customer.getorders().add(order1);
  customer.getorders().add(order2);
  this.savecustomer(customer);
  
  }
  
  public static void main(string[] args) {
  
  try{
  new test().testmethod();
  
  }catch(exception e)
  {
  system.out.println("this is the testmethod throw exception.....");
  e.printstacktrace();
  }
  
  }
  }
  
  ok,下面是一个hibernate的配置hibernate.cfg.xml(位于应用目录下面)
  
  <!doctype hibernate-configuration public
  "-//hibernate/hibernate configuration dtd 3.0//en"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  <hibernate-configuration>
  <session-factory>
  <property name="hibernate.connection.url">
  jdbc:inetdae:localhost:1433?charset=gbk&database=hibernate_test
  </property>
  <property name="hibernate.connection.driver_class">
  com.inet.tds.tdsdriver
  </property>
  <property name="hibernate.connection.username">
  sa
  </property>
  <property name="hibernate.connection.password">
  aa
  </property>
  <property name="hibernate.dialect">
  org.hibernate.dialect.sqlserverdialect
  </property>
  <property name="show_sql">
  true
  &
 
 
上一篇: java学习之hibernate配置要点详谈    下一篇: hibernate中多对多关系的常见问题
  相关文档
java基础:java日期加减法 11-17
java 程序中的多线程(三) 11-17
eclipse中用swt和jface开发入门 11-16
java更新xml的四种常用方法简介(1) 11-16
理论与实践: 用弱引用堵住内存泄漏 11-17
虚拟机概论(六)——java虚拟机模型 11-17
将java的class文件转为exe的八种方法 11-17
k700之图像内存处理 11-17
spring2.0中新的bean类型实现原理 11-17
看java编程思想笔记(5-6) 11-17
在struts中使用plugin扩展hibernate 03-24
用java 接口实现回调函数的等价功能 11-17
java混淆器retroguard的使用方法 11-17
理解subjects, principals and credentials 11-17
xml 和 java 11-17
struts框架中实现多行表单提交 11-17
java/jsp学习系列之改写mysql翻页例子 11-17
精通ejb【七】 11-17
java 国际化和本地化 toolkit 2.0(下) 11-17
jvm 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息