论坛首页 Java企业应用论坛

使用one-to-many关系遇到的奇怪问题

浏览 5356 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-03-03  
数据库结构
parent表
parentid
parentname
child表
childid
parentid
childname
其中parentid为外键


Parent.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.websh.hibernate.model.Parent" table="parent" dynamic-update="false"
        dynamic-insert="false">
<id column="parentid" name="parentid" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="parentname" length="50" name="parentname" type="java.lang.String"/>

        <set name="childs" cascade="all-delete-orphan">
            <key column="parentid"/>
            <one-to-many class="com.websh.hibernate.model.Child"/>
        </set>

</class>
</hibernate-mapping>


Child.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.websh.hibernate.model.Child" table="child" dynamic-update="false"
        dynamic-insert="false">
<id column="childid" name="childid" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="parentid" length="10" name="parentid" type="java.lang.Integer"/>
<property column="childname" length="50" name="childname" type="java.lang.String"/>

<many-to-one name="parent" class="com.websh.hibernate.model.Parent" column="parentid" insert="false" update="false"/>

</class>
</hibernate-mapping>




Parent.java
package com.websh.hibernate.model;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import java.util.Collection;
import java.util.Set;

/** @author Hibernate CodeGenerator */
public class Parent implements Serializable {

    /** identifier field */
    private Long parentid;

    /** nullable persistent field */
    private String parentname;

    private Collection childs;

    /** full constructor */
    public Parent(String parentname); {
        this.parentname = parentname;
    }

    /** default constructor */
    public Parent(); {
    }



    public Collection getChilds(); {
        return this.childs;
    }

    public void setChilds(Collection childs); {
        this.childs = childs;
    }


    public Long getParentid(); {
        return this.parentid;
    }

    public void setParentid(Long parentid); {
        this.parentid = parentid;
    }

    public String getParentname(); {
        return this.parentname;
    }

    public void setParentname(String parentname); {
        this.parentname = parentname;
    }

    public String toString(); {
        return new ToStringBuilder(this);
            .append("parentid", getParentid(););
            .toString();;
    }

    public boolean equals(Object other); {
        if ( !(other instanceof Parent); ); return false;
        Parent castOther = (Parent); other;
        return new EqualsBuilder();
            .append(this.getParentid();, castOther.getParentid(););
            .isEquals();;
    }

    public int hashCode(); {
        return new HashCodeBuilder();
            .append(getParentid(););
            .toHashCode();;
    }

}




Child.java
package com.websh.hibernate.model;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class Child
    implements Serializable {

  /** identifier field */
  private Long childid;

  /** nullable persistent field */
  private Integer parentid;

  /** nullable persistent field */
  private String childname;

  private Parent parent;

  /** full constructor */
  public Child(Integer parentid, String childname); {
    this.parentid = parentid;
    this.childname = childname;
  }

  /** default constructor */
  public Child(); {
  }

  public Long getChildid(); {
    return this.childid;
  }

  public void setChildid(Long childid); {
    this.childid = childid;
  }

  public Integer getParentid(); {
    return this.parentid;
  }

  public void setParentid(Integer parentid); {
    this.parentid = parentid;
  }

  public String getChildname(); {
    return this.childname;
  }

  public void setChildname(String childname); {
    this.childname = childname;
  }

  public String toString(); {
    return new ToStringBuilder(this);
        .append("childid", getChildid(););
        .toString();;
  }

  public boolean equals(Object other); {
    if (! (other instanceof Child););return false;
    Child castOther = (Child); other;
    return new EqualsBuilder();
        .append(this.getChildid();, castOther.getChildid(););
        .isEquals();;
  }

  public int hashCode(); {
    return new HashCodeBuilder();
        .append(getChildid(););
        .toHashCode();;
  }

  public Parent getParent(); {
    return parent;
  }

  public void setParent(Parent parent); {
    this.parent = parent;
  }

}





测试方法
package com.websh.hibernate.test;

import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import com.websh.hibernate.model.Parent;
import com.websh.hibernate.model.Child;

import java.util.*;

public class RelTest {
  public RelTest(); {
  }
  
  public static void create();{
    Set al = new HashSet();;
    try{
      Configuration cfg = new Configuration();.configure();;
      SessionFactory sessions = cfg.buildSessionFactory();;
      Session session = sessions.openSession();;
      Transaction tx = session.beginTransaction();;

      Parent p = new Parent();;
      p.setParentname("父亲");;

      Child c1 = new Child();;
      c1.setChildname("儿子1");;

      Child c2 = new Child();;
      c2.setChildname("儿子2");;

      al.add(c1);;
      al.add(c2);;

      p.setChilds(al);;
      session.save(p);;

      tx.commit();;
      session.close();;
    }catch(Exception e);{
      e.printStackTrace();;
    }
  }
  

  public static void update();{
    Set al = new HashSet();;
    try{
      Configuration cfg = new Configuration();.configure();;
      SessionFactory sessions = cfg.buildSessionFactory();;
      Session session = sessions.openSession();;
      Transaction tx = session.beginTransaction();;

      Parent p = new Parent();;
      p.setParentname("父亲update");;
      p.setParentid(new Long(2););;
      
      Child c1 = new Child();;
      c1.setParent(p);;
      c1.setChildname("儿子1update");;

      Child c2 = new Child();;
      c2.setParent(p);;
      c2.setChildname("儿子2update");;

      al.add(c1);;
      al.add(c2);;

      p.setChilds(al);;
      session.update(p);;

      tx.commit();;
      session.close();;
    }catch(Exception e);{
      e.printStackTrace();;
    }
  }


  public static void remove();{
    ArrayList al = new ArrayList();;
    try{
      Configuration cfg = new Configuration();.configure();;
      SessionFactory sessions = cfg.buildSessionFactory();;
      Session session = sessions.openSession();;
      Transaction tx = session.beginTransaction();;

      Parent p = new Parent();;
      p.setParentid(new Long(2););;

      session.delete(p);;

      tx.commit();;
      session.close();;
    }catch(Exception e);{
      e.printStackTrace();;
    }
  }


  public  static void  load();{
    ArrayList al = new ArrayList();;
    try{
      Configuration cfg = new Configuration();.configure();;
      SessionFactory sessions = cfg.buildSessionFactory();;
      Session session = sessions.openSession();;

      Parent pp = (Parent);session.load(Parent.class, new Long(2););;
      System.out.println("Parent name :" + pp.getParentname(););;
      for (Iterator iter = pp.getChilds();.iterator();; iter.hasNext();; ); {
        Child item = (Child);iter.next();;
        System.out.println("Child name : "+item.getChildname(););;
      }
      session.close();;
    }catch(Exception e);{
      e.printStackTrace();;
    }

  }



  public  static void  main(String[] args);{

    create();;


  }
}


级联的load没有问题
现在问题是child的parentid字段不能设置为not null如果这样在所有更新操作都出错
<set name="childs" cascade="all-delete-orphan">
改为<set name="childs" cascade="all">也一样
加上inverse="true"也不管用
如果把字段改为可以为null
则可以创建
但是child表只有一个记录就是"儿子1"的记录,作update或者delete的时候也不是把child的字段删除而是把parentid字段变为null
不知道哪里出错了
   发表时间:2004-03-03  
现在增和删都可以了
就差update了
哪位能帮忙看看 
0 请登录后投票
   发表时间:2004-03-03  
update的时候每次子表都是新增操作老的记录不删除的
主表可以更新
0 请登录后投票
   发表时间:2004-03-03  
相关设置
&lt;bag name="childs" cascade="all" inverse="true" lazy="false" &gt;
0 请登录后投票
   发表时间:2004-03-12  
private Integer parentid;把parenid该为字符型可能你的问题就能解决,具体为什么我也在找原因中
0 请登录后投票
   发表时间:2004-03-30  
*********************************
老大,能不能告诉我新增时为什么只能保存一个啊?
我也碰到同样问题!
*********************************
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics