[英]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. 在将依赖项从Spring 4.1.6.RELEASE更新到5.1.4.RELEASE并将Hibernate 4.3.9.Final更新到5.4.1.Final之后,我的一些测试用例因主键上的ConstraintViolationException而开始失败。
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. 我注意到,ConstraintViolationException仅发生在在同一测试用例中插入多个值的表中。 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 我发现了这一点: Hibernate,@ SequenceGenerator和allocationSize
Which made me find "hibernate.id.new_generator_mappings" 这让我找到了“ hibernate.id.new_generator_mappings”
As per: https://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#configurations 按照: 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. *此设置指示新的org.hibernate.id.IdentifierGenerator是否用于AUTO,TABLE和SEQUENCE。
Existing applications may want to disable this (set it false) for upgrade compatibility from 3.x and 4.x to 5.x.* 现有应用程序可能想要禁用此功能(将其设置为false),以实现从3.x和4.x到5.x的升级兼容性。*
Setting this to false solved the problem. 将此设置为false可解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.