开发者

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

开发者 https://www.devze.com 2023-02-10 08:39 出处:网络
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_INCREME

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 preme开发者_如何学JAVAditation by developers (will not be implemented because not)?


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消