lfyy1 2009-01-29 21:10
浏览 243
已采纳

因为many-to-one,插入子对象错误

表article的sectorname 是表 sector(主键名也是sectorname)的外键
[color=red]Article.hbm.xml[/color]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >


name="Article"
table="ARTICLE"
>
false
name="Topic"
column="TOPIC"
type="string"
length="40"
>


<!-- name="Sectorname"
column="SECTORNAME"
type="string"
not-null="true"
length="40"
/> -->
name="sector"
column="sectorname
class="com.struts_learn.data.Sector"
/>


[color=red]代码:[/color]
Sector sector = new Sector();
sector.setSectorname("vu5");
sector.setCreateid("aaa");
sector.setCreatetime(new Date());
Article article = new Article();
article.setTopic("fadg5");
article.setSector(sector);
session.save(article);
tx.commit();

[color=red]log:[/color]Hibernate: insert into ARTICLE (SECTORNAME,TOPIC) values (?, ?)
报错:Caused by: java.sql.BatchUpdateException: ORA-01400: 无法将 NULL 插入 ("AB"."ARTICLE"."SECTORNAME")

             <!-- <property
        name="Sectorname"
        column="SECTORNAME"
        type="string"
        not-null="true"
        length="40"
    /> -->

把mapping文件注释去掉
[color=red]代码: [/color]

Sector sector = new Sector();
sector.setSectorname("vu5");
sector.setCreateid("aaa");
sector.setCreatetime(new Date());
Article article = new Article();
article.setTopic("fadg5");
article.setSector(sector);
article.setSectorname("vu5");
session.save(article);
tx.commit();

[color=red]log:Hibernate:[/color] insert into ARTICLE (SECTORNAME, sectorname, TOPIC) values (?, ?, ?)
报错:Caused by: java.sql.BatchUpdateException: ORA-00957: 列名重复

搞半天没明白,到底哪配错了...

  • 写回答

1条回答 默认 最新

  • iteye_2372 2009-02-01 00:59
    关注

    设置cascade='all'或者'saveOrUpdate'或者你先save你的sector然后save多的那个

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?