请教高手:ssh中一对多数据关联时,为什么一方无法正常插入数据,多方则可以正常插入?
业务类别(ywlb)与一级指标(yjzb)是一对多数据关系,Ywlb.hbm.xml文件如下:
<hibernate-mapping>
<class name="pbc.xjtac.zfjc.vo.Ywlb" table="ywlb" catalog="zfjc">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="ywlb" type="java.lang.String">
<column name="ywlb" length="45" />
</property>
<property name="qz" type="java.lang.Float">
<column name="qz" precision="12" scale="0" />
</property>
<set name="yjzbs" cascade="all" inverse="true">
<key>
<column name="ywlb_id"></column>
</key>
<one-to-many class="pbc.xjtac.zfjc.vo.Yjzb"></one-to-many>
</set>
</class>
</hibernate-mapping>
Yjzb.hbm.xml的映射文件如下:
<hibernate-mapping>
<class name="pbc.xjtac.zfjc.vo.Yjzb" table="yjzb" catalog="zfjc">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="yjzb" type="java.lang.String">
<column name="yjzb" length="45" />
</property>
<set name="rjzbs" cascade="all"> //此处是建立与二级指标一对多关联关系
<key>
<column name="yjzb_id"></column>
</key>
<one-to-many class="pbc.xjtac.zfjc.vo.Rjzb"/>
</set>
</class>
</hibernate-mapping>
JgxxAction如下:
public class JgxxAction {
private String ywlb;
private Float qz;
private String yjzb;
Setter、Getter方法省略
//增加业务项目
public String addYwlb(){
Ywlb ywlbs =new Ywlb();
ywlbs.setYwlb(ywlb);
ywlbs.setQz(qz);
if(jgxxService.findYwlb(ywlb)==null){
jgxxService.addYwlbS(ywlbs);
return "addywlbS";
}else{
return "addywlbF";
}}
//增加一级指标
public String addYjzb(){
Yjzb yjzbs = new Yjzb();
yjzbs.setYjzb(yjzb);
if(jgxxService.findYjzbS(yjzb)==null){
jgxxService.addYjzbS(yjzbs);
return "addyjzbS";
}else{
return "addyjzbF";
}
}}
JgxxDaoHiberImpl文件如下
public class JgxxDaoHibImpl extends HibernateDaoSupport implements JgxxDao {
//查找业务项目名称
public Ywlb findYwlbD(String ywlb){
List<Ywlb> list =this.
getHibernateTemplate().find("from Ywlb where ywlb =?",ywlb);
if(list.size()>0){
return list.get(0);
}
return null;
}
//增加业务类别
public void addYwlbD(Ywlb ywlb){
this.getHibernateTemplate().persist(ywlb);
}
//查找一级指标名称
public Yjzb findYjzbD(String yjzb){
List<Yjzb> list = this.getHibernateTemplate().find("from Yjzb where yjzb = ?",yjzb);
if(list.size()>0){
return list.get(0);
}else{
return null;
}}
//增加一级指标
public void addYjzbD(Yjzb yjzb){
this.getHibernateTemplate().persist(yjzb);