Hibernate does not fill AUTO_INCREMENT column being part of composite PK, bug or anti-feature?

风格不统一 提交于 2019-12-23 14:25:32

问题


I've found a problem with Hibernate and composite key with one of its column auto incremented.

I'm using MySQL and primary key composed of 2 columns, GID and LANG. GID column is set as AUTO_INCREMENT. Insert is working, hibernate inserts new row, MySQL adds auto_generated id when GID is null. However, Hibernate doesn't set the GID field in object given to persist.

I've heard of problems generally connected with using composite primary key with hibernate, so I'm asking if this is a bug in hibernate, or an anti-feature left with premeditation by developers (will not be implemented because not)?


回答1:


The Hibernate Annotations Reference Guide for 3.5.6-Final (Section 2.2.3.2.4. Partial identifier generation) said that it is possible to use Sequences for a part of a composite id.

2.2.3.2.4. Partial identifier generation

Hibernate supports the automatic generation of some of the identifier properties. Simply use the @GeneratedValue annotation on one or several id properties.

Warning: The Hibernate team has always felt such a construct as fundamentally wrong. Try hard to fix your data model before using this feature.

(There is an example in the Docu!)

But partenon is right too: the Hibernate Reference Guid (section 8.4. Components as composite identifiers) for the same version said that is is not possible (for xml).

You cannot use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.

I don't know if there is a difference between Annotation and XML mapping.

<irony>I start to love hibernate more and more.</irony>




回答2:


It's not a bug. This is the desired behavior, as you can see in the documentation:

You cannot use an IdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.

http://docs.jboss.org/hibernate/stable/core/reference/en/html/components.html#components-compositeid



来源:https://stackoverflow.com/questions/5003596/hibernate-does-not-fill-auto-increment-column-being-part-of-composite-pk-bug-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!