hibernate(双向连接配置)

 简捷开发
     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

       双向关联

     +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
     实例1、
     ----------------------------------------------------------
  ----------------------------------------------------------
     one-to-many /many-to-one

     类文件 添加属性
  (many)
  private Set customerId=new HashSet();
     (one)
  private User userId;
     映射文件
  (many)
      <set name="customerId" fetch="join" lazy="false" cascade="save-update,persist" >
        <key column="userId"
        not-null="false"/>
        <one-to-many
        class="com.feng.pojo.Customer" />
       </set>
  (one)
     
     测试类:
   Customer c=new Customer("tudou","123");
  Customer c2=new Customer("tudou","123");
  User u=new User("digua","123");
  Set coustomers=new HashSet();
  coustomers.add(c);
  coustomers.add(c2);
  u.setCustomerId(coustomers);
   Configuration configuration=new Configuration();
  configuration.configure("/hibernate.cfg.xml");
  SessionFactory sessionFactory=configuration.buildSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction trans=session.beginTransaction();
  session.save(c);
  session.save(u);
  ----------------------------------------------------------
  ----------------------------------------------------------
     one-to-one

     类文件
  (one)
  private Customer customerId;
     (one)
  private User userId;
   映射文件
   (one)
   <many-to-one  name="customerId"
       unique="true"
       not-null="true"/>
    (one)
 <one-to-one name="userId" class="com.feng.pojo.User"/>
     测试类:
 
  Customer c=new Customer("tudou","123");
  Customer c2=new Customer("tudou","123");
  User u=new User("digua","123");
  User u2=new User("digua","123");
  u.setCustomerId(c);
  u2.setCustomerId(c2);
  
   Configuration configuration=new Configuration();
  configuration.configure("/hibernate.cfg.xml");
  SessionFactory sessionFactory=configuration.buildSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction trans=session.beginTransaction();
  session.save(c);
  session.save(c2);
  session.save(u);
  session.save(u2);
     ----------------------------------------------------------
  ----------------------------------------------------------
 
  one-to-one(基于主键关联的 one-to-one)

     类文件
  (one)
  private Customer customerId;
     (one)
  private User userId;
   映射文件
   (one)
   <one-to-one name="customerId" cascade="all" class="com.feng.pojo.Customer"/>
    (one)
     <id name="customerId">
      <generator class="foreign">
      <param name="property">userId</param>
      </generator>
      </id>
 <one-to-one name="userId" constrained="true" class="com.feng.pojo.User"/>
     测试类:
 
   Customer c=new Customer("tudou","123");
  User u=new User("digua","123");
        c.setUserId(u);
  u.setCustomerId(c);
  
  
   Configuration configuration=new Configuration();
  configuration.configure("/hibernate.cfg.xml");
  SessionFactory sessionFactory=configuration.buildSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction trans=session.beginTransaction();
  session.save(c);
  session.save(u);
      ----------------------------------------------------------
   ----------------------------------------------------------
    ????????????????????????????
   ----------------------------------------------------------
   ----------------------------------------------------------
  <!--使用连接表的-->
 
  实例2、
  <!-- one-to-many/many-to-one -->
  类文件 添加属性:
  (one)
  private Set customerId=new HashSet();
  (many)
  private User userId;
  映射文件:
     (one)
  <set name="customerId" fetch="join" lazy="false" cascade="save-update,persist" >
        <key column="userId"
        not-null="true"/>
        <many-to-many unique="true"
        class="com.feng.pojo.Customer" />
       </set>
    (many)
    <join table="jointab"
       inverse="true" optional="true">
       <key column="cstomerId"/>
       <many-to-one name="userId" column="userId" not-null="true"/>
       </join>
    测试类:
    Customer c=new Customer("tudou","123");
  Customer c2=new Customer("tudou","123");
  User u=new User("digua","123");
  Set customers=new HashSet();
  customers.add(c);
  customers.add(c2);
        c.setUserId(u);
        u.setCustomerId(customers);
  
  
  
   Configuration configuration=new Configuration();
  configuration.configure("/hibernate.cfg.xml");
  SessionFactory sessionFactory=configuration.buildSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction trans=session.beginTransaction();
  session.save(c);
  session.save(u);
     ----------------------------------------------------------
  ----------------------------------------------------------
    <!-- one-to-one-->
   
  类文件 添加属性:
  (one)
  private Customer customerId;
  (one)
  private User userId;
  映射文件:
  (one)
   <join
       table="jointab"
       optional="true"
       >
       <key column="userId" unique="true"/>
       <many-to-one name="customerId" column="customerId" not-null="true" unique="true" />
       </join>
    (one)
    <join table="jointab"
inverse="true"
optional="true">
<key column="customerId"
unique="true"/>
<many-to-one name="userId"
column="userId"
not-null="true"
unique="true"/>
</join>
    测试类:
    Customer c=new Customer("tudou","123");
  Customer c2=new Customer("tudou","123");
  User u=new User("digua","123");
      
        u.setCustomerId(c);
        c.setUserId(u);
 
  
   Configuration configuration=new Configuration();
  configuration.configure("/hibernate.cfg.xml");
  SessionFactory sessionFactory=configuration.buildSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction trans=session.beginTransaction();
  
  session.save(u);
  session.save(c);
     ----------------------------------------------------------
  ----------------------------------------------------------
   
      <!-- many-to-many(双向)-->
   
  类文件 添加属性:
  相关类添加属性 例如:
  private Set CustomerId;
  private Set userId=new HashSet();
 
  相关类添加
  映射文件:
  (many-to-many)
  <set name="customerId" cascade="save-update" lazy="false" table="jointable">
        <key column="userId"/>
        <many-to-many column="customerId"
        class="com.feng.pojo.Customer"/>
        </set>
     (many-to-many)
   <set name="userId" cascade="save-update" lazy="false" table="joinb">
        <key column="customerId" />
        <many-to-many class="com.feng.pojo.User" column="userId"/>
        </set>
    测试类:
   
    Customer c=new Customer("tudou","123");
  Customer c2=new Customer("tudou","123");
  User u=new User("digua","123");
  User u2=new User("digua","123");
  Set customers=new HashSet();
  Set users=new HashSet();
  users.add(u);
  users.add(u2);
  customers.add(c);
  customers.add(c2);
  u.setCustomerId(customers);
  c.setUserId(users);
  
   Configuration configuration=new Configuration();
   configuration.configure("/hibernate.cfg.xml");
   SessionFactory sessionFactory=configuration.buildSessionFactory();
   Session session=sessionFactory.openSession();
   Transaction trans=session.beginTransaction();
   session.save(c);
   session.save(u);
   
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

       双向关联

       +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

posted @ 2013-11-29 10:23  黑云吐雾  阅读(103)  评论(0编辑  收藏  举报