Search code examples
mysqljpapersistenceeclipselink

GenerationType and MySQL Errors


I have web form where user inserts datas. Then there are stored in the database. But I have got a problem with record in db. I use spring 3, jpa, eclipselink and mysql. In entity @Id is generated. When I use strategy: GenerationType.AUTO, GenerationType.TABLE datas are not stored and error:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Base table or view not found, message from server: "Table 'sklep.sequence' doesn't exist" Error Code: 1146 Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?bind => [2 parameters bound]
When use GenerationType.IDENTITY, GenerationType.SEQUENCE, only the first data are storing and then : javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: null, message from server: "Duplicate entry '0' for key 'PRIMARY'" Error Code: 1062
What is going on? Is this problem with mysql or something? Help! Thanks!


Solution

  • You need to create a table SEQUENCE when using TABLE id generation. If you use EclipseLink to generate your DDL it will be created for you. You also need to insert a row for each generator name.

    For IDENTITY id generation you need to use an IDENTITY type for your id when creating your table.

    See, http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Sequencing