简体   繁体   中英

ConstraintViolationException using TableGenerator after updating dependencies

After updating dependencies from Spring 4.1.6.RELEASE to 5.1.4.RELEASE and Hibernate 4.3.9.Final to 5.4.1.Final, some of my test cases started failing with ConstraintViolationException on the primary key.

Due to historical reasons we are generating the primary like so:

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "NAME_HERE")
@TableGenerator(name = "NAME_HERE", table = "SEQUENCE_TABLE_NAME", pkColumnName = "Name", pkColumnValue = "ENTITY_NAME", valueColumnName = "VALUE_COLUMN_NAME", allocationSize = 100)

While using the application normally it does not seem to be a problem, however when running tests it does.

I have noticed that the ConstraintViolationException only occurs for tables where multiple values are inserted within the same test case. So i'm thinking the rows are somehow assigned the same primary key.

Running the tests locally it does not seem to be a problem, so it could also have something to do with how the tests are run on the build server. However it was not a problem before upgrading dependencies.

I have checked that the sequence values are higher than the latest value in the database, but again this was not a problem before upgrading dependencies.

I am hoping to jolt someones memory :-)

i found this: Hibernate, @SequenceGenerator and allocationSize

Which made me find "hibernate.id.new_generator_mappings"

As per: https://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#configurations

*Setting which indicates whether or not the new org.hibernate.id.IdentifierGenerator are used for AUTO, TABLE and SEQUENCE.

Existing applications may want to disable this (set it false) for upgrade compatibility from 3.x and 4.x to 5.x.*

Setting this to false solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM