Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » java.sql.SQLIntegrityConstraintViolationException: Duplicate entry when persisting an entity
java.sql.SQLIntegrityConstraintViolationException: Duplicate entry when persisting an entity [message #1869111] Thu, 01 August 2024 07:10 Go to next message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 214
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Hi
I am working on an application using eclipselink to connect to a mysql database.

I have the problem that when I try to persist a fresh created entity, then I get an IntegrityConstraintViolationException on primary key (for key 'role.PRIMARY'). The exception is always thrown, indipendently of which entity class is persisted.

There exists a @MappedSuperclass with the following header:

@MappedSuperclass
@UuidGenerator(name = "UUID_GEN")
@EntityListeners(JpaEntityMediator.class)
@ObjectTypeConverter(
		name = "booleanConverter", dataType = java.lang.Short.class, objectType = java.lang.Boolean.class, conversionValues = {
				@ConversionValue(dataValue = "0", objectValue = "false"), @ConversionValue(dataValue = "1", objectValue = "true") }, defaultObjectValue = "false"
)
public abstract class JpaAbstractEntity implements Entity<UUID>
{


The following is an example of a concrete entity:

//@formatter:off
@Entity
@Table(name = "role")
@UuidGenerator(name = "UUID_GEN")
@AttributeOverrides({
	@AttributeOverride(name="id", column=@Column(name="role_id")),
	@AttributeOverride(name="version", column=@Column(name="role_version")),
	@AttributeOverride(name="deleted", column=@Column(name="role_deleted")),
	@AttributeOverride(name="deletedBy", column=@Column(name="role_deleted_by")),
	@AttributeOverride(name="created", column=@Column(name="role_created")),
	@AttributeOverride(name="createdBy", column=@Column(name="role_created_by")),
	@AttributeOverride(name="updated", column=@Column(name="role_updated")),
	@AttributeOverride(name="updatedBy", column=@Column(name="role_updated_by")),
})
//@formatter:on
public class JpaRole extends JpaAbstractEntity implements Role
{


The code to persist an entity looks like this:

	@Override
	public <E extends Entity<UUID>> void persist(E entity)
	{
		EntityTransaction transaction = this.entityManager.getTransaction();
		try
		{
			transaction.begin();
			this.entityManager.persist(entity);
			transaction.commit();
		}
		catch (EntityExistsException e)
		{
			transaction.rollback();
			throw e;
		}
		catch (RollbackException e)
		{
			transaction.rollback();
			throw e;
		}
		finally
		{
		}
	}


and the Exception thrown looks like this:

jakarta.persistence.RollbackException: jakarta.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 4.0.3.v202405161415): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'e475e076-4fd1-11ef-9cb7-dcdfc642f713' for key 'role.PRIMARY'
Error Code: 1062
Call: INSERT INTO role (role_id, role_authorized, role_created, role_deleted, role_name, role_predefined_role, role_predefined_source, role_updated, role_version, CREATEDBY_user_id, DELETEDBY_user_id, role_selected_profile, UPDATEDBY_user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	bind => [13 parameters bound]
Query: InsertObjectQuery(ch.eugster.railcontrol.persistence.jpa.model.JpaRole@f151da55)


When I examine the database, there is no existing primary key with the given in the exception message. Also before entityManager.persist(entity) is called, the uuid of the entity to persist is null.

As in the header of the mapped superclass can be seen, I use a entityMediator, but this does not manipulate the id attribute.

I stepped with debugger through the code, but could not find any solution for days. If more information is used I gladly give more information. Thank you.
Re: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry when persisting an entity [message #1874440 is a reply to message #1869111] Tue, 07 January 2025 17:46 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 60
Registered: December 2021
Member
This is old, but if the database is clear, check out all the statements being issued and the parameters as it might show more on the problem, such as two inserts for the same ID - see https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging to turn on logging. I would guess the issue is with what you are not showing - relationships, the ID mapping, etc. From the sounds of this, it should be reproducible with a simple, small entity test case; you might want to start there and build it up to match what you have until you can reproduce it.

Best Regards,
Chris
Previous Topic:java.sql.SQLException: Closed Resultset: next
Next Topic:NoInitialContextException
Goto Forum:
  


Current Time: Thu Jan 23 21:45:33 GMT 2025

Powered by FUDForum. Page generated in 0.02683 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top