Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bastaware opened this issue Apr 1, 2018 · 5 comments · Fixed by #375
Open

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

bastaware opened this issue Apr 1, 2018 · 5 comments · Fixed by #375

Comments

@bastaware
Copy link

bastaware commented Apr 1, 2018

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; ...

@ludofischer
Copy link
Contributor

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

@facos86
Copy link

facos86 commented Aug 16, 2018

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
...

@Jeetman
Copy link

Jeetman commented Nov 16, 2018

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.

@newsfusion
Copy link

had the same issuce, thanks @ludovicofischer

@tzachs
Copy link

tzachs commented Oct 10, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants