`

hibernate 3配置

    博客分类:
  • SSH2
 
阅读更多

hibernate 搭建

1、所需jar包
antlr-2.7.6.jar、commons-collections-3.1.jar、dom4j-1.6.1.jar、hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、
javassist-3.11.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar

2、src下hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="foo">
 <property name="show_sql">true</property>
 <property name="myeclipse.connection.profile">oraclejdbc</property>
 <property name="connection.url">
  jdbc:oracle:thin:@10.8.205.70:1521:orcl
 </property>
 <property name="connection.username">sspm</property>
 <property name="connection.password">sspm</property>
 <property name="connection.driver_class">
  oracle.jdbc.driver.OracleDriver
 </property>
 <property name="dialect">
  org.hibernate.dialect.Oracle9Dialect
 </property>
 <mapping resource="com/sspm/hibernate/test/Customer.hbm.xml"/>
 <mapping resource="com/sspm/hibernate/test/Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>


3、编写HibernateUtil.java
public class HibernateUtil {

 private static SessionFactory sessionFactory;

 private HibernateUtil() {
 }

 static {
  Configuration cfg = new Configuration();
  cfg.configure();
  sessionFactory = cfg.buildSessionFactory();
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 public static Session getSession() {
  return sessionFactory.openSession();
 }
}

4、数据库表

表customer
-- Create table
create table CUSTOMER
(
  ID   NUMBER not null,
  NAME VARCHAR2(20)
)
tablespace FM
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints
alter table CUSTOMER
  add constraint COUSTOMER_PKID primary key (ID)
  disable;

表ORDERS
-- Create table
create table ORDERS
(
  ID          NUMBER not null,
  ORDERNUMBER VARCHAR2(20),
  CUSTOMERID  NUMBER not null
)
tablespace FM
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints
alter table ORDERS
  add constraint ORDER_PKID primary key (ID)
  disable;
alter table ORDERS
  add constraint CUSTOMER_FK foreign key (CUSTOMERID)
  references CUSTOMER (ID) on delete cascade
  disable;

5、javabean 及映射文件
Customer.java
public class Customer {

 private Long id;
 private String name;
 private Set<Order> orders = new HashSet<Order>();
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Set<Order> getOrders() {
  return orders;
 }
 public void setOrders(Set<Order> orders) {
  this.orders = orders;
 }
}

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.sspm.hibernate.test.Customer" table="Customer">
  <id name="id" type="java.lang.Long" column="Id">
   <generator class="sequence">
    <param name="sequence">SQ_CUSTOMER_ID</param> 
   </generator>
  </id>
  <property name="name" column="Name" type="string"></property>
  <set name="orders" cascade="all" inverse="true">
   <key column="CustomerId" /><!-- 对应着外键 -->
   <one-to-many class="com.sspm.hibernate.test.Order" />
  </set>
 </class>
</hibernate-mapping>

public class Order {

 private Long id;
 private Customer customer;
 private String orderNumber;
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public Customer getCustomer() {
  return customer;
 }
 public void setCustomer(Customer customer) {
  this.customer = customer;
 }
 public String getOrderNumber() {
  return orderNumber;
 }
 public void setOrderNumber(String orderNumber) {
  this.orderNumber = orderNumber;
 }
}

Order.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="com.sspm.hibernate.test.Order" table="Orders">
  <id name="id" type="java.lang.Long" column="Id">
   <generator class="sequence">
    <param name="sequence">SQ_ORDER_ID</param> 
   </generator>
  </id>
  <property name="orderNumber" column="OrderNumber"
   type="string">
  </property>
  <many-to-one name="customer" column="CustomerId"
   class="com.sspm.hibernate.test.Customer" cascade="all" lazy="false"
   not-null="true">
  </many-to-one>
 </class>
</hibernate-mapping>

6、action 测试类
public class CustomerAction {

  private Customer customer;
  private List<Customer> listCustomer;

  public Customer getCustomer() {
   return customer;
  }

  public void setCustomer(Customer customer) {
   this.customer = customer;
  }

  public List<Customer> getListCustomer() {
   return listCustomer;
  }

  public void setListCustomer(List<Customer> listCustomer) {
   this.listCustomer = listCustomer;
  }

  /** * 添加客户 * */
  public void addCustomer(Customer customer) {
   Session s = null;
   Transaction tx = null;
   try {
    s = HibernateUtil.getSession();
    tx = s.beginTransaction();
    s.save(customer);
    tx.commit();
   } catch (Exception e) {
    if (tx != null) {
     tx.rollback();
    }
    e.printStackTrace();
   } finally {
    if (s != null) {
     s.close();
    }
   }
  }

  /** * 删除客户 * */
  public void deleteCustomer(Customer customer) {
   Session s = null;
   Transaction tx = null;
   try {
    s = HibernateUtil.getSession();
    tx = s.beginTransaction();
    s.delete(customer);
    tx.commit();
   } catch (Exception e) {
    if (tx != null) {
     tx.rollback();
    }
    e.printStackTrace();
   } finally {
    if (s != null) {
     s.close();
    }
   }
  }

  /** * 更新客户 * */
  public void update(Customer customer, String name) {
   Session s = null;
   Transaction tx = null;
   try {
    s = HibernateUtil.getSession();
    tx = s.beginTransaction();
    customer.setName(name);
    s.update(customer);
    tx.commit();
   } catch (Exception e) {
    if (tx != null) {
     tx.rollback();
    }
    e.printStackTrace();
   } finally {
    if (s != null) {
     s.close();
    }
   }
  }

  /** * 查询客户 * */
  public Customer findCustomer(Long id) {
   Session s = null;
   Transaction tx = null;
   try {
    s = HibernateUtil.getSession();
    tx = s.beginTransaction();
    customer = (Customer) s.get(Customer.class, id);
    tx.commit();
   } catch (Exception e) {
    if (tx != null) {
     tx.rollback();
    }
    e.printStackTrace();
   } finally {
    if (s != null) {
     s.close();
    }
   }
   return customer;
  }

  /** * 查找所有的客户 * */
  public List<Customer> findAll() {
   Session s = null;
   Transaction tx = null;
   try {
    s = HibernateUtil.getSession();
    tx = s.beginTransaction();
    Query query = s
      .createQuery("from Customer as a order by id asc");
    listCustomer = query.list();
    for (Iterator iter = listCustomer.iterator(); iter.hasNext();) {
     Customer customer = (Customer) iter.next();
     System.out.println("客户ID是:" + customer.getId() + "客户姓名是:"
       + customer.getName());
    }
    tx.commit();
   } catch (Exception e) {
    if (tx != null) {
     tx.rollback();
    }
    e.printStackTrace();
   } finally {
    if (s != null) {
     s.close();
    }
   }
   return listCustomer;
  }
}

public class OrderAction {
 private Order order;
 private List<Order> listorder;

 public Order getorder() {
  return order;
 }

 public void setorder(Order order) {
  this.order = order;
 }

 public List<Order> getListorder() {
  return listorder;
 }

 public void setListorder(List<Order> listorder) {
  this.listorder = listorder;
 }

 public void addorder(Order order) {
  Session s = null;
  Transaction tx = null;
  try {
   s = HibernateUtil.getSession();
   tx = s.beginTransaction();
   s.save(order);
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   e.printStackTrace();
  } finally {
   if (s != null) {
    s.close();
   }
  }
 }

 /** * 删除用户 * */
 public void deleteorder(Order order) {
  Session s = null;
  Transaction tx = null;
  try {
   s = HibernateUtil.getSession();
   tx = s.beginTransaction();
   s.delete(order);
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   e.printStackTrace();
  } finally {
   if (s != null) {
    s.close();
   }
  }
 }

 public void update(Order order, String number) {
  Session s = null;
  Transaction tx = null;
  try {
   s = HibernateUtil.getSession();
   tx = s.beginTransaction();
   order.setOrderNumber(number);
   s.update(order);
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   e.printStackTrace();
  } finally {
   if (s != null) {
    s.close();
   }
  }
 }

 public Order findorder(Long id) {
  Session s = null;
  Transaction tx = null;
  try {
   s = HibernateUtil.getSession();
   tx = s.beginTransaction();
   order = (Order) s.get(Order.class, id);
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   e.printStackTrace();
  } finally {
   if (s != null) {
    s.close();
   }
  }
  return order;
 }

 public List<Order> findAll() {
  Session s = null;
  Transaction tx = null;
  try {
   s = HibernateUtil.getSession();
   tx = s.beginTransaction();
   Query query = s.createQuery("from Order as a order by id asc");
   listorder = query.list();
   for (Iterator iter = listorder.iterator(); iter.hasNext();) {
    Order order = (Order) iter.next();
    System.out.println("订单ID是:" + order.getId() + "订单数目是:"
      + order.getOrderNumber());
   }
   tx.commit();
  } catch (Exception e) {
   if (tx != null) {
    tx.rollback();
   }
   e.printStackTrace();
  } finally {
   if (s != null) {
    s.close();
   }
  }
  return listorder;
 }
}

public class Test {
 public static void main(String args[]) {
  Customer customer = new Customer();
  customer.setName("google");
  CustomerAction ca = new CustomerAction();
  /**
   * * 添加对象 *
   */
  ca.addCustomer(customer);

  OrderAction oa = new OrderAction();
  Order order = new Order();
  order.setOrderNumber("77");
  order.setCustomer(customer);
  oa.addorder(order);
 }
}

分享到:
评论
1 楼 heipacker 2014-03-13  
标题党!!

相关推荐

Global site tag (gtag.js) - Google Analytics