`
xiaofengtoo
  • 浏览: 484063 次
  • 性别: Icon_minigender_1
  • 来自: xiamen
社区版块
存档分类
最新评论

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

    博客分类:
  • DB
阅读更多

引用一篇文章 来自:MeChecksV

 

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():

今天学的HIBERNATE,回去自己练习,可是却遇到了上面的问题。老办法,百度,很幸运,第一个链接我就得到了解决问题的方法。

-----------------------------------------------------------------------------------------

引起问题的原因:

就是那个由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"问题便解决。

 

 

以下是我碰到问题

<id name="id" type="java.lang.String">
            <column name="ID" length="8" />
            <generator class="sequence" >
            	<param name="sequence">BA_A_ALLOWEDIP_ID</param>
            </generator>
        </id>

 后来发现跟sequence 写法也有很大关系,大家如果以后碰到不妨往这个方向考虑一二。

分享到:
评论

相关推荐

    for linux PyV8-0.7.zip

    安装emmet不生效。PyV8是一个Python的V8引擎谷歌的包装,它充当了Python和JavaScript对象的桥梁,支持申办谷歌的V8引擎在一个Python脚本。

    EurekaLog_7.5.0.0_Enterprise

    2)....Fixed: Range check error in processes information for x64 machines (affects startup of any EurekaLog-enabled module) 3)....Fixed: Auto-detect personality by project extension if --el_mode switch...

    qt动态场景源码

    qt的动态场景实现源码,基于c++实现,可以作为很好的学习参考

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    If this is enabled, by calling the VsEnableImplicitPalette() function, the driver will define the settings for a given wavelength once, then saves the results within the VariSpec for faster access ...

    dev_ids.tsv

    glue_data的MRPC数据集映射文件

    Python For ArcGIS 数据

    sh and invertebrates.Performing this process manually for one dataset could be time consuming and error prone. Performing this manually for numerous datasets is completely imprac-tical. Common GIS ...

    acpi控制笔记本风扇转速

    ACPI_OPERAND_OBJECT has been manually optimized to be aligned and will not work if it is byte-packed. Example Code and Data Size: These are the sizes for the OS- independent acpica.lib produced by ...

    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 ...

    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 ...

    Solution for Designer error when opening WorldWind.cs in WW1.4.0

    When I load the WW ... The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. ...

    seagrid-master.zip

    SeaGrid is a Matlab 5.2+ application for generating an orthogonal grid within a curved perimeter, suitable for oceanographic modeling. With SeaGrid, one can: Select a conformal map projection. ...

    ULPBench - Beyond the Data Sheets of Ultra-Low-Power MCU.pdf

    Note: the USB driver must be manually installed because the USB driver is not recognized by the PC. The appropriate drivers are located in the /bin/USB_CDC folder. Connect the terminals +3 V (VCC) ...

    gerrit-3.0.3.war

    Utility script remove-notedb-refs.sh which can be used to remove all refs created for NoteDb in case of rollback to ReviewDb. Issue 11356: Allow to override autodetected Lucene index configuration. ...

    BURNINTEST--硬件检测工具

    - Minor changes to the No operation error watchdog timer for the CD and Hard disk tests. - Minor correction to the Butterfly seek test. - Video playback trace logging increased. Release 5.3 build ...

    UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY

    主要介绍了UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    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 ...

    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 ...

    Oracle sqldeveloper without jdk (win+linux)

    If the locale of the operating system is not English, a few UI elements, like default wizard buttons and Oracle error messages, may show up in the language of this locale. If the locale of the ...

    matlab2012注册破解文件

    1) choose "install manually without using the internet" 2) enter the "file installation key" 38699-60149-36808-21840-05491 3) use "license_standalone.dat" when asked for license file network 1) ...

    MATLAB 2009A LINCE CRACK

    1) choose "install manually without using the internet" 2) enter the "file installation key" 11111-11111-02011-44270 3) use "license_standalone.dat" when asked for license file network 1) choose ...

Global site tag (gtag.js) - Google Analytics