熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

開發框架 hibernate3.0 開發實例

2022-06-13   來源: Java開源技術 

  建議是有一點點是一點點基礎的人又沒有hibernate基礎和經驗的人比較適合(所謂一點點基礎是最起碼不要我介紹一些配置文件的什麼的)注意我用的JDBC驅動cominettdsTdsDriver
  
  大家一起努力學習吧
  
  建立PO對象
  
  建PO對象CustomerOrder以及它們的hibernate配置文件
  
  /*
  * Created on
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */
  package testdemo;
  
  import javautilHashSet;
  import javautilSet;
  
  /**
  * @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) {
  thisusername = username;
  thispassword = password;
  
  thisorders = 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) {
  thisid = id;
  }
  
  public void setPassword(String password) {
  thispassword = password;
  }
  
  public void setUsername(String username) {
  thisusername = username;
  }
  
  public void setOrders(Set orders) {
  thisorders = orders;
  }
  
  }
  
  <?xml version=?>
  <!DOCTYPE hibernatemapping PUBLIC
  //Hibernate/Hibernate Mapping DTD //EN
  mappingdtd>
  <hibernatemapping>
  <class name=testdemoCustomer table=CUSTOMER dynamicinsert=true dynamicupdate=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=saveupdate
  >
  <key column=CUSTOMER_ID />
  <onetomany class=testdemoOrder />
  </set>
  
  </class>
  </hibernatemapping>
  
  /*
  * Created on
  *
  * 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 javaioSerializable;
  public class Order implements Serializable {
  
  private Long id;
  private String orderNumber;
  private double price;
  private Customer customer;
  
  public Order() {
  }
  public Order(String orderNumberdouble priceCustomer customer) {
  thisorderNumber=orderNumber;
  thisprice=price;
  thiscustomer=customer;
  }
  public Long getId() {
  return thisid;
  }
  
  public void setId(Long id) {
  thisid = id;
  }
  
  public String getOrderNumber() {
  return thisorderNumber;
  }
  
  public void setOrderNumber(String orderNumber) {
  thisorderNumber = orderNumber;
  }
  
  public Customer getCustomer() {
  return thiscustomer;
  }
  
  public void setCustomer(Customer customer) {
  thiscustomer = customer;
  }
  
  public double getPrice(){
  return thisprice;
  }
  private void setPrice( double price ){
  thisprice = price;
  }
  }
  
  <?xml version=?>
  <!DOCTYPE hibernatemapping PUBLIC
  //Hibernate/Hibernate Mapping DTD //EN
  mappingdtd>
  
  <hibernatemapping package=testdemo>
  
  <class name=Order table=ORDERS>
  
  <id name=id>
  <generator class=increment/>
  </id>
  
  <property name=orderNumber column=ORDER_NUMBER/>
  <property name=price />
  
  <manytoone
  name=customer
  column=CUSTOMER_ID
  class=Customer
  notnull=true
  />
  
  </class>
  </hibernatemapping>
  
  上面的PO已經建立完成下面是一個測試類
  
  /*
  * Created on
  *
  * TODO To change the template for this generated file go to
  * Window Preferences Java Code Style Code Templates
  */
  package testdemo;
  
  import javautilHashSet;
  
  import orghibernateSession;
  import orghibernateSessionFactory;
  import orghibernateTransaction;
  import orghibernatecfgConfiguration;
  
  import testdemoCustomer;
  
  /**
  * @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(nfigure()addClass(
  testdemoCustomerclass)addClass(Orderclass)buildSessionFactory();
  Session session = sessionFactoryopenSession();
  public void saveCustomer(Customer customer) throws Exception {
  
  Transaction tx = null;
  try {
  tx = sessionbeginTransaction();
  sessionsave(customer);
  
  mit();
  
  } catch (Exception e) {
  if (tx != null) {
  txrollback();
  }
  throw e;
  } finally {
  sessionclose();
  }
  }
  
  public void testmethod()throws Exception
  {
  Customer customer=new Customer(lzhengjnew HashSet());
  Order order=new Order(Ordercustomer);
  Order order=new Order(Ordercustomer);
  customergetOrders()add(order);
  customergetOrders()add(order);
  thissaveCustomer(customer);
  
  }
  
  public static void main(String[] args) {
  
  try{
  new Test()testmethod();
  
  }catch(Exception e)
  {
  Systemoutprintln(this is the testmethod throw exception);
  eprintStackTrace();
  }
  
  }
  }
  
  ok下面是一個hibernate的配置hibernatecfgxml(位於應用目錄下面)
  
  <!DOCTYPE hibernateconfiguration PUBLIC
  //Hibernate/Hibernate Configuration DTD //EN
  configurationdtd>
  <hibernateconfiguration>
  <sessionfactory>
  <property name=nnectionurl>
  jdbc:inetdae:localhost:?charset=gbk&database=hibernate_test
  </property>
  <property name=nnectiondriver_class>
  cominettdsTdsDriver
  </property>
  <property name=nnectionusername>
  sa
  </property>
  <property name=nnectionpassword>
  aa
  </property>
  <property name=hibernatedialect>
  orghibernatedialectSQLServerDialect
  </property>
  <property name=show_sql>
  true
  &
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28232.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.