`
尘枉_yjava
  • 浏览: 74344 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

ids for this class must be manually assigned before calling save()..

    博客分类:
  • web
阅读更多
引起问题的原因:
由Hibernate根据数据库表自动生成的"类名.hbm.xml"映射文件引起的。
首先我的表(Info)由两个字段组成,即:
int id;//主建
String name;
(自己做测试,所以就简单的建了个表)
由Hibernate生成的Info.hbm.xml中是这样写的:
-----------------------------------------------------
<id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned"/>
</id>
-----------------------------------------------------
<id>这个是必须有的。它是用来定义实体的标识属性(对应数据库表的主键)
而我这里由于id本身就是主键,所以column的属性便是id
下面是很关键的一点<generator>,由于一时兴趣,于是找了很多资料,关于它的解释是:用于指定主键的生成策略。它的值有多,下面是转来的:
--------------------------------------------------------------------------------
“assigned”
主键由外部程序负责生成,在   save()   之前指定一个。
  
“hilo”
通过hi/lo   算法实现的主键生成机制,需要额外的数据库表或字段提供高位值来源。
  
“seqhilo”
与hilo   类似,通过hi/lo   算法实现的主键生成机制,需要数据库中的   Sequence,适用于支持   Sequence   的数据库,如Oracle。
  
“increment”
主键按数值顺序递增。此方式的实现机制为在当前应用实例中维持一个变量,以保存着当前的最大值,之后每次需要生成主键的时候将此值加1作为主键。这种方式可能产生的问题是:不能在集群下使用。
  
“identity”
采用数据库提供的主键生成机制。如DB2、SQL   Server、MySQL   中的主键生成机制。
  
“sequence”
采用数据库提供的   sequence   机制生成主键。如   Oralce   中的Sequence。
  
“native”
由   Hibernate   根据使用的数据库自行判断采用   identity、hilo、sequence   其中一种作为主键生成方式。
  
“uuid.hex”
由   Hibernate   基于128   位   UUID   算法   生成16   进制数值(编码后以长度32   的字符串表示)作为主键。
  
“uuid.string”
与uuid.hex   类似,只是生成的主键未进行编码(长度16),不能应用在   PostgreSQL   数据库中。
    
“foreign”
使用另外一个相关联的对象的标识符作为主键。
--------------------------------------------------------------------------------
看了上面的介绍,再看看代码,原来是<Generator>属性设置有问题。
然后改为"identity"、"native"问题便解决。
分享到:
评论

相关推荐

    SSH整合项目中容易出现的错误

    1. org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save() 这个错误是由于在Hibernate的mapping文件中,元素没有正确配置,缺少了其子元素的配置...

    ssh框架报错分析集合

    1. `org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save()` 这个异常通常是因为Hibernate在尝试保存对象时,发现对象的ID没有被正确地设置或生成...

    hibernate常见问题及处理.doc

    4. `org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():` 此异常意味着在尝试保存对象前,对象的主键没有被正确设置。通常,这是由于在映射...

    框架集合错误解决方案

    org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save() ``` **问题描述:** 当尝试保存一个实体对象时,如果该对象的`id`字段没有被手动赋值,...

    工作周报10221

    - **Hibernate逆向工程**:解决`ids for this class must be manually assigned`错误,修改了自动生成的ID配置,添加了`dynamic-update`和`dynamic-insert`属性以优化数据库操作。 - **Spring与Hibernate集成**:...

    EurekaLog_7.5.0.0_Enterprise

    This also reduces memory footprint for ecc32/emake 8)....Added: Showing call stacks for 2 new types of fatal memory errors 9)....Added: EMemLeaks._ReserveOutOfMemory to control reserve size of out of...

    Houdini12_5keygenByFlipMoneky.exe

    BEFORE STARTING HOUDINI: 3. Stop HoudiniLicenseServer (from services) 4. Copy sesinted.exe from Crack dir to c:\windows\system32 (overwrite) 5. Start HoudiniLicenseServer (from services) 6.Licensing...

    rubygems-master-(1).zip_GEM

    You must have an installation of RubyGems before this update can be applied. See Gem for information on RubyGems (or `ri Gem`) To upgrade to the latest RubyGems, run: $ gem update --system # you ...

    dev_ids.tsv

    glue_data的MRPC数据集映射文件

    QC英文教程PDF文档

    Before You Begin ...................................................................................9 Documentation Library.......................................................................10 ...

    Developing Flex Applications 910p dda_doc88_cracker.zip

    This package contains 3 kid: 1. a book Developing Flex Applications 2. a web page viewer for doc88 ebt 3. a DDA downloader for doc88.com CONTENTS PART I: Presenting Flex CHAPTER 1: Introducing ...

    QTP11补丁发布 - 支持64位IE浏览器

    1. Before installing this patch, you must install the QTP_00699 patch. 2. This patch does not provide 64-bit support for other Web-based environments, for example .NET WebForms, ActiveX, Siebel, ...

    vb写的ole 回调函数

    'manually and everything should be set for your client app to call the server through 'OLE. 'Note6: Every time you build a new exe of your server, VB will generate a new 'unique ID for each of its ...

    美军网络战防御武器,强大的重组包能力

    Pygeoip is not yet available as a package and must be installed with pip or manually. sudo apt-get install python-crypto python-dpkt python-ipy python-pypcap sudo pip install pygeoip 2、Configure ...

    mpeg4ip source code

    mpeg4ip can be used to generate track hint for mp4 file, this is a must for Darwin Streaming Server. mpeg4ip source code and SDL source code for convenient download. The SDL is a must for Configure ...

    Delphi7.1 Update

    breaking information not included in this readme file.IMPORTANT: Delphi must be closed before installing this update. =====================================================CONTENTS * INSTALLING THIS ...

    Python For ArcGIS 数据

    After that, the next assignment will be to answer the same question based on monthly species inventory datasets for fi sh and invertebrates.Performing this process manually for one dataset could be ...

    apktool documentation

    We must get HTC framework resources before decoding this apk. We pull com.htc.resources.apk from our device and install it $ apktool if com.htc.resources.apk I: Framework installed to: 2.apk Now we ...

    UE(官方下载)

    This is a convenient feature when you're manually comparing files, when you want to copy/paste between multiple files, or when you simply want to divide up your edit space. Tabbed Child Windows ...

    VclZip pro v3.10.1

    The default for this property is False and if left like this VCLZip will behave like normal. If set to True, VCLZip will encrypt each buffer prior to compressing it instead of afterwards. This will ...

Global site tag (gtag.js) - Google Analytics