论坛首页 Java企业应用论坛

一个关于继承+一对多的错误

浏览 3685 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-03-02  
一个继承,加一对多的类关系,
class square,line 都继承了Sharp,而square又由line组成,sharp.hbm.xml:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
	<class discriminator-value="A" name="com.bearingpoint.gdc.hibernate.example.Sharp">
	    <!--hibernate为我们生成主键id-->
		<id name = "id" unsaved-value = "null">
			<generator class="identity"/>
		</id>
		
        <property name="name"/>
	    <!--1:n关系的映射-->

        <joined-subclass  name="com.bearingpoint.gdc.hibernate.example.Line">
            <key column="id" /> 
            <property name="startpoint" type="int" />
            <property name="endpoint" type="int" />

        </joined-subclass>

        <joined-subclass  name="com.bearingpoint.gdc.hibernate.example.Square">
            <key column="id" /> 
            <property name="length" type="int" />

            <bag name="lines" table="line" cascade="all"> 
                <key column="square_id"/> 
                <one-to-many class="com.bearingpoint.gdc.hibernate.example.Line"/> 
            </bag>
        </joined-subclass>

	</class>
</hibernate-mapping>

执行一下操作时,发生错误
		Line line1=new Line();;
		line1.setName("line1");;
		line1.setStartpoint(0);;
		line1.setEndpoint(10);;
		
		Line line2=new Line();;
		line2.setName("line2");;
		line2.setStartpoint(11);;
		line2.setEndpoint(20);;

		List lines=new Vector();;
		lines.add(line1);;
		lines.add(line2);;
		
		square.setLines(lines);;

		s.save(square);;//此句出错

net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded): [com.bearingpoint.gdc.hibernate.example.Line#0]
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1281)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1206)
at net.sf.hibernate.engine.Cascades$3.cascade(Cascades.java:88)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:298)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:739)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
at com.bearingpoint.gdc.hibernate.example.TestSharp.main(TestSharp.java:69)
Exception in thread "main"

发现数据库里square表里已经插入一条数据,当line表里没有数据
谁能帮帮我呀
   发表时间:2004-03-03  
贴出line的映射及代码
0 请登录后投票
   发表时间:2004-03-03  
public class Line extends Sharp {

	/**
	 * 
	 */
	int id;
	int startpoint;
	int endpoint;
	
	public Line(); {
		super();;
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return
	 */
	public int getEndpoint(); {
		return endpoint;
	}

	/**
	 * @return
	 */
	public int getId(); {
		return id;
	}

	/**
	 * @return
	 */
	public int getStartpoint(); {
		return startpoint;
	}

	/**
	 * @param i
	 */
	public void setEndpoint(int i); {
		endpoint = i;
	}

	/**
	 * @param i
	 */
	public void setId(int i); {
		id = i;
	}

	/**
	 * @param i
	 */
	public void setStartpoint(int i); {
		startpoint = i;
	}

	public boolean equals(Object o); { 
	 if (this == o); 
	   return true; 
	 if (! (o instanceof Line);); 
	   return false; 
	 Line c = (Line); o; 
	 return this.id==c.id; 
	} 

	public int hashCode(); { 
	 return super.hashCode();; 
	} 
}

public class Square extends Sharp {
	int length;
	int id;
	List lines=new java.util.Vector();;
	/**
	 * 
	 */
	public Square(); {
		super();;
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return
	 */
	public int getId(); {
		return id;
	}

	/**
	 * @return
	 */
	public int getLength(); {
		return length;
	}

	/**
	 * @param i
	 */
	public void setId(int i); {
		id = i;
	}

	/**
	 * @param i
	 */
	public void setLength(int i); {
		length = i;
	}

	/**
	 * @return
	 */
	public List getLines(); {
		return lines;
	}

	/**
	 * @param set
	 */
	public void setLines(List set); {
		lines = set;
	}

	public boolean equals(Object o); { 
	 if (this == o); 
	   return true; 
	 if (! (o instanceof Square);); 
	   return false; 
	   Square c = (Square); o; 
	 return this.id==c.id; 
	} 

	public int hashCode(); { 
	 return super.hashCode();; 
	} 
	
}

line,square都没写hbm.xml,都在sharp.hbm.xml里写了
yehs220真是个大好人,上海的聚会你去吗?到时当面谢谢你
0 请登录后投票
   发表时间:2004-03-05  
仔细看看你的代码吧,编译都通不过!
0 请登录后投票
   发表时间:2004-03-05  
我放上我的eclipse  project包:)
0 请登录后投票
   发表时间:2004-03-08  
我没运行你程序,我想问题关键是你用到cascade,主子表之间必须有关联。你试试这个行不行,注意Line的many-to-one,Square的key、one-to-many的inverse。


        <joined-subclass  name="com.bearingpoint.gdc.hibernate.example.Line"> 
            <key column="id" /> 
            <property name="startpoint" type="int" /> 
            <property name="endpoint" type="int" /> 
           <many-to-one name="square" column="square_id" class="com.bearingpoint.gdc.hibernate.example.Square"/>
        </joined-subclass> 

        <joined-subclass  name="com.bearingpoint.gdc.hibernate.example.Square"> 
            <key column="id" /> 
            <property name="length" type="int" /> 

            <bag name="lines" table="line" cascade="all"> 
                <key column="square_id"/> //应该是Line表中的字段
                <one-to-many class="com.bearingpoint.gdc.hibernate.example.Line"
inverse="true"/> 
            </bag> 
        </joined-subclass> 


另外,在Squre中除了加入相应方法外,还需要在Square加入类似下面的方法:
public void addLine(Line line);
{
     line.setSquare(this);;
     add(line);;
}
0 请登录后投票
   发表时间:2004-03-08  
不好意思,Square中的inverse="true",应该放到bag中,不是one-to-many
0 请登录后投票
   发表时间:2004-03-09  
太感谢了:)
0 请登录后投票
论坛首页 Java企业应用版

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