play1 icon indicating copy to clipboard operation
play1 copied to clipboard

Duplicate entry for key 'PRIMARY' With Play 1.5.0 + MySql

Open bastaware opened this issue 7 years ago • 5 comments

Ran into some trouble trying to upgrade 1.4.x to 1.5.0 - can't seem to find a migration guide?

After upgrading, when I save a class extending play.db.jpa.Model e.g.:

new Transaction(transDate, TransactionType.STOCK_BOUGHT, share, price, null).save();

The id of the record collides with existing entry and I get execeptions like:

Duplicate entry '9' for key 'PRIMARY'

At first it appeared to me that auto_increment of the table had been reset. But inserting manually worked fine.

Found this explanation: https://vladmihalcea.com/why-should-not-use-the-auto-jpa-generationtype-with-mysql-and-hibernate/

Should play.db.jpa.Model be updated to use native, or should I simply make my own Model class ala:

@MappedSuperclass public class MyModel extends GenericModel { @Id @GeneratedValue( strategy= GenerationType.AUTO, generator="native" ) @GenericGenerator( name = "native", strategy = "native" ) public Long id; ...

bastaware avatar Apr 01 '18 18:04 bastaware

Try disabling the new hibernate ID generator by adding this option to application.conf:

hibernate.id.new_generator_mappings=false

More information: https://developer.jboss.org/thread/267613

ludofischer avatar Jun 21 '18 18:06 ludofischer

Hi, Any idea of how to configure hibernate.id.new_generator_mappings=false in multi database environment ? hibernate.id.new_generator_mappings=false is working well for the primary database, but it not working for others databases configured in my project

Ps I have tried : db.database2.hibernate.id.new_generator_mappings=false database2.hibernate.id.new_generator_mappings=false ...

facos86 avatar Aug 16 '18 13:08 facos86

Hey man,

I work for a website for my school and our entire database was broken because we couldn't save anything. You have saved us all. You are a hero.

Jeetman avatar Nov 16 '18 03:11 Jeetman

had the same issuce, thanks @ludovicofischer

newsfusion avatar Jan 24 '19 14:01 newsfusion

Thank you @bastaware and @ludofischer for this thread, saved me hours!

tzachs avatar Oct 10 '22 20:10 tzachs