0 0

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

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

<hibernate-mapping package="com.hibernate123.data">
<class
name="Article"
table="ARTICLE"
>
<meta attribute="sync-DAO">false</meta>
<id
name="Topic"
column="TOPIC"
type="string"
length="40"
>
<generator class="assigned"></generator>
</id>
<!-- <property
name="Sectorname"
column="SECTORNAME"
type="string"
not-null="true"
length="40"
/> -->
<many-to-one
   name="sector"
   column="sectorname
   class="com.struts_learn.data.Sector"
/>
</class>
</hibernate-mapping>

代码:
                  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();

log: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文件注释去掉
代码:             
                  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();

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

搞半天没明白,到底哪配错了...
2009年1月29日 21:10

1个答案 按时间排序 按投票排序

0 0

采纳的答案

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

2009年2月01日 00:59

相关推荐

    Hibernate性能调优

    关联通常包括以下几种类型:单向`one-to-many`关联、双向`one-to-many`关联、`many-to-one`关联以及`one-to-one`关联。 ##### 单向`one-to-many`关联 - **定义**:单向`one-to-many`关联是指一个实体可以拥有多个...

    hibernate5--3.映射关系

    本文将深入探讨Hibernate5中的映射关系,主要包括多对多(Many-to-Many)和一对多(One-to-Many)这两种关系。 一、多对多映射(Many-to-Many) 多对多关系在数据库中表现为两个表之间存在多个连接记录,而在对象...

    Hibernate中cascade和inverse应用

    在 Hibernate 框架中,`cascade` 和 `inverse` 是两个重要的概念,它们主要用于管理对象之间的持久化关系,特别是涉及到一对一(one-to-one)、一对多(one-to-many)和多对多(many-to-many)关系时。这两个属性都...

    hibernate(一对多,多对一映射)操作

    这种关系在数据库中可以表现为一对多(One-to-Many)或多对一(Many-to-One)的关系。 1. 一对多映射(One-to-Many): 在这种关系中,一个实体(父实体)可以与多个其他实体(子实体)相关联。在Hibernate中,...

    hibernate 双向 多对对,一对多,一对一 映射的 集合

    而在子类中,通常会有一个反向引用的`&lt;many-to-one&gt;`标签,指向父类。这样,我们就可以通过父类集合获取所有子类实例,或者通过子类的引用找到父类。 接下来,一对一映射(`&lt;one-to-one&gt;`)代表了两个实体之间一对...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate-mapping

    例如,`&lt;one-to-one&gt;`、`&lt;many-to-one&gt;`、`&lt;one-to-many&gt;`和`&lt;many-to-many&gt;`元素分别对应这四种关系。关联映射通常涉及外键的处理和级联操作。 5. 注解方式的Mapping 除了XML映射,Hibernate还支持注解方式的映射...

    HibernateAPI中文版.chm

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    hibernate3.2中文文档(chm格式)

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate中文详细学习文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    Hibernate中List信息的配置

    Set映射通常通过`&lt;set&gt;`标签来定义,同样可以包含`&lt;key&gt;`和`&lt;one-to-many&gt;`子标签,但不会包含`&lt;index&gt;`标签,因为Set本身不关心元素的插入顺序。 ### Map映射 Map映射是另一种重要的集合映射方式,它可以处理一对...

    hibernate 体系结构与配置 参考文档(html)

    一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...

    Hibernate 中文 html 帮助文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...

    Hibernate+中文文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...

    最全Hibernate 参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    hibernate 框架详解

    一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...

    Hibernate3+中文参考文档

    6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...

    hibernate3.04中文文档.chm

    22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...

    Hibernate教程

    22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...

    常用 Hibernate 映射配置说明.doc

    `many-to-one`节点表示多对一的关系,通常涉及外键引用。 #### 8. 多对多关联 `many-to-many`节点处理多对多的复杂关联,可能需要额外的关联表。 通过以上详尽的解释,可以看出Hibernate映射配置的强大和灵活性,...

Global site tag (gtag.js) - Google Analytics