丄諦啲仇魜ヤ
如 果 敌 人 让 你 生 气 , 那 说 明 你 没 有 胜 他 的 把 握!
posts - 6,comments - 56,trackbacks - 1

创建 db_iteam表 

    create table db_iteam(
          id  int identity not null,
         iteamNum"char(10) not null,
         paId int null,
         primary key ("id")
    )
go

/*************/
创建db_user表

    create table db_user (
        id   int identity not null,
       name char(10) not null,
       iteamId int null,
        primary key ("id")
    )
/***********************/
创建db_passport
 create table db_passport(
        "id" int not null,  //主键关联与db_user
       "number" char(10) not null,
        primary key ("id")
    )
go
*******************
               vo:
*******************
package wsq.vo;

import java.util.HashSet;
import java.util.Set;

public class DbIteam implements java.io.Serializable {

 private Integer id;

 private String iteamNum;
 private DbIteam subIteam;

 private Set subIteams=new HashSet();
 
 private Set users=new HashSet();

 public Set getUsers() {
  return users;
 }

 public void setUsers(Set users) {
  this.users = users;
 }
 public DbIteam() {
 }
 public DbIteam(String iteamNum) {
  this.iteamNum = iteamNum;
 }

 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getIteamNum() {
  return this.iteamNum;
 }

 public void setIteamNum(String iteamNum) {
  this.iteamNum = iteamNum;
 }

 public DbIteam getSubIteam() {
  return subIteam;
 }

 public void setSubIteam(DbIteam subIteam) {
  this.subIteam = subIteam;
 }

 public Set getSubIteams() {
  return subIteams;
 }

 public void setSubIteams(Set subIteams) {
  this.subIteams = subIteams;
 }

}


******************

package wsq.vo;

public class DbUser implements java.io.Serializable {

 private Integer id;

 private String name;
   
 private DbIteam dbIteam;
 private DbPassport  dbPassport;

    
 public DbPassport getDbPassport() {
  return dbPassport;
 }

 public void setDbPassport(DbPassport dbPassport) {
  this.dbPassport = dbPassport;
 }

  public DbUser() {
 }
 public DbUser(String name) {
  this.name = name;
 }

 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }
 public String toString()
 {
  return name+"   测试!";
 }

 public DbIteam getDbIteam() {
  return dbIteam;
 }

 public void setDbIteam(DbIteam dbIteam) {
  this.dbIteam = dbIteam;
 }

}

/**********************************/

package wsq.vo;

public class DbPassport implements java.io.Serializable {

 private Integer id;

 private String number;

 private DbUser dbUser;

 public DbUser getDbUser() {
  return dbUser;
 }

 public void setDbUser(DbUser dbUser) {
  this.dbUser = dbUser;
 }

 public DbPassport() {
 }
 public DbPassport(String number) {
  this.number = number;
 }

 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getNumber() {
  return this.number;
 }

 public void setNumber(String number) {
  this.number = number;
 }

}

/******************************/
配置DbIteam.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">
<!--
 Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
 <class name="wsq.vo.DbIteam" table="db_iteam" schema="dbo"
  catalog="dbs" >
  <id name="id" type="java.lang.Integer">
   <column name="id" />
   <generator class="native" />
  </id>
  <property name="iteamNum" type="java.lang.String">
   <column name="iteamNum" length="10" not-null="true" />
  </property>
  
   <many-to-one name="subIteam" class="wsq.vo.DbIteam" column="paId"  /> 
 
  
  <set name="users" cascade="all" table="db_user" lazy="true" batch-size="5">
   <key column="iteamId"></key>
   <one-to-many class="wsq.vo.DbUser" />
  </set>
  <set name="subIteams" cascade="all" table="db_iteam" lazy="true" batch-size="5">
   <key column="paId"></key>
   <one-to-many class="wsq.vo.DbIteam" />
  </set>
 </class>
</hibernate-mapping>


配置DbUser.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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="wsq.vo.DbUser" table="db_user" schema="dbo" catalog="dbs">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="10" not-null="true" />
        </property>
        
        <many-to-one name="dbIteam" class="wsq.vo.DbIteam" column="iteamId" outer-join="true"  ></many-to-one>
       
        <one-to-one name="dbPassport" class="wsq.vo.DbPassport" cascade="all" ></one-to-one>
    </class>
</hibernate-mapping>

配置DbPassport.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">
<!--
 Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
 <class name="wsq.vo.DbPassport" table="db_passport" schema="dbo"
  catalog="dbs">
  <id name="id" type="java.lang.Integer">
   <column name="id" />
   <generator class="foreign" >
   <param name="property">dbUser</param>
   </generator>
  </id>
  <property name="number" type="java.lang.String">
   <column name="number" length="10" not-null="true" />
  </property>
  <one-to-one name="dbUser" class="wsq.vo.DbUser" constrained="true"></one-to-one>
 </class>
</hibernate-mapping>


/*******************
测试类

package wsq.vo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;

public class Test {
 private static final Log log = LogFactory.getLog(Test.class);

 Test() {

 }

 public static void main(String[] args) {
  BaseHibernateDAO basedao = new BaseHibernateDAO( );
  DbIteamDAO iteamDAO = new DbIteamDAO( );
  DbUserDAO userDAO = new DbUserDAO( );

  DbIteam iteam = new DbIteam( );
  DbIteam iteam3 = new DbIteam( );
  
  DbUser user = new DbUser( );
  DbPassport ps = new DbPassport( );
  
  Transaction tx = basedao.getSession( ).beginTransaction( );
  
  iteam.setIteamNum("2");
  
  iteam.setSubIteam(iteamDAO.findById(16));   //iteamDAO.findById(16)找到的是一级  此处添加是2级
  
  
  
  iteam3.setIteamNum("3");
  
  iteam3.setSubIteam(iteam);//3级
  
  user.setName("djg");

  user.setDbIteam(iteam3);

  ps.setNumber("48");

  user.setDbPassport(ps);

  ps.setDbUser(user);

  
  // iteamDAO.delete(iteamDAO.findById(23));
  iteamDAO.save(iteam);
  iteamDAO.save(iteam3);
  userDAO.save(user);
  log.info("ok!!!!!!    !!ok!!!  ok!     111111111111111111  !!  !");
  tx.commit(  );

 }

}








 

posted on 2007-09-02 10:21 Crying 阅读(1104) 评论(0)  编辑  收藏 所属分类: hibernate

只有注册用户登录后才能发表评论。


网站导航: