Compound ID with auto-generated part produces exception

Description

2. creating database and table by hand
#mysql -u username ...
create table F_TESTROW (ID bigint not null auto_increment, VERSION bigint not null, primary key (VERSION, ID))

TestRow testRow = new TestRow();
testRow.setVersion(2L);
testRow.setAddr("somewhere");
sessionFactory.openSession().save(testRow);

produces
java.lang.IllegalArgumentException: Can not set java.lang.Long field ru.kctsoft.fenestro.domain.strategy.TestRow.id to org.hibernate.id.IdentifierGeneratorHelper$2
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:139)

This is kinda frustrating.

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/
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.

Environment

java 6.23 64bit
Hibernate 3.5.6

Activity

Show:
Rafael Rodrigues de Souza
March 1, 2017, 2:46 PM

MySQL most definitely allows AUTO_INCREMENT in composite keys! That is the reason I came here years ago! And it not allows it, as it enforces it when using partitioned tables.

Chris Cranford
March 1, 2017, 4:19 PM
Edited

, I probably should have mentioned MySQL with InnoDB. The MyISAM implementation in MySQL does support it unless this has since changed recently.

Rafael Rodrigues de Souza
March 1, 2017, 5:19 PM

Chris, I also meant InnoDB. It does allow AUTO_INCREMENT using composite keys, as long as the AUTO_INCREMENT column is the first one of the key (that is the difference to MyISAM).

Stefan Höltker
December 16, 2019, 10:24 AM

Same Problem with Hibernate 5.2.16 and SQL Server IDENTITY

IT Support
April 21, 2020, 1:07 PM

Is this a duplicate of HHH-9662?

Assignee

Unassigned

Reporter

AlexandrS

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure