首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java对象持久化学习2

2012-10-30 
Java对象持久化学习二1. 一对多的对象关系 customer and order一个customer有多个订单public class Custom

Java对象持久化学习二
1. 一对多的对象关系 customer and order

    一个customer有多个订单

    public class Customer {          private Set orders=new HashSet();        public Customer() {}        public Set getOrders() {           return this.orders;        }        public void setOrders(Set orders) {           this.orders = orders;        }    }


2. 一对一的对象关系

    一个订单只能属于一个客户
    public class Order {       private Customer customer;           public Order() {}       public Customer getCustomer() {           return this.customer;       }       public void setCustomer(Customer customer) {          this.customer = customer;       }    }

3. 当持久化对象时,应该考虑与它关联的对象.

   添加一个新的客户对象
  
  public void saveCustomer(Customer customer) throws Exception {    Connection con=null;    PreparedStatement stmt=null;    try {      con=getConnection();            con.setAutoCommit(false);            if(customer.getName()==null)        throw new BusinessException("");                  long customerId=getNextId(con,"CUSTOMERS");      //首先向数据库添加customer对象      stmt=con.prepareStatement("insert into CUSTOMERS(ID,NAME,AGE) values(?,?,?)");      stmt.setLong(1,customerId);      stmt.setString(2,customer.getName());      stmt.setInt(3,customer.getAge());      stmt.execute();             //得到customer对象的所有订单,向数据库中添加      Iterator iterator =customer.getOrders().iterator();      while (iterator.hasNext() ) {                Order order=(Order)iterator.next();        if(order.getOrderNumber()==null)          throw new BusinessException("");               long orderId=getNextId(con,"ORDERS");                stmt=con.prepareStatement("insert into ORDERS(ID,ORDER_NUMBER,PRICE,CUSTOMER_ID)values(?,?,?,?)");        stmt.setLong(1,orderId);        stmt.setString(2,order.getOrderNumber());        stmt.setDouble(3,order.getPrice());        stmt.setLong(4,customerId);        stmt.execute();      }            con.commit();    }catch(Exception e){      e.printStackTrace();      try{        con.rollback();      }catch(SQLException sqlex){        sqlex.printStackTrace(System.out);      }      throw e;    }finally{      try{         stmt.close();         con.close();       }catch(Exception e){         e.printStackTrace();       }    }  }

4. 删除customer对象时也应该考虑到这种关系
     public void deleteCustomer(Customer customer) throws Exception {    Connection con=null;    PreparedStatement stmt=null;    try {      con=getConnection();             con.setAutoCommit(false);      //先删除订单      stmt=con.prepareStatement("delete from ORDERS where "                            +"CUSTOMER_ID=?" );      stmt.setLong(1,customer.getId().longValue());      stmt.executeUpdate();      //然后删除客户对象      stmt=con.prepareStatement("delete from CUSTOMERS where "                         +"ID=?" );      stmt.setLong(1,customer.getId().longValue());      stmt.executeUpdate();            con.commit();    }catch(Exception e){      try{        con.rollback();      }catch(SQLException sqlex){        sqlex.printStackTrace(System.out);      }      throw e;    }finally{      try{         stmt.close();         con.close();       }catch(Exception e){         e.printStackTrace();       }    }  }

热点排行