|PersistenceException upon flush() - constraint violation [message #949196]
||Thu, 18 October 2012 19:55
| Julie Schenk
Registered: August 2012
My question at the end is: I found a solution - does it seem like an acceptable solution?
Profile.java has the following:
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
and is included by Plant.java
@OneToOne(cascade = CascadeType.PERSIST)
private Profile profile;
A Profile must exist before a Plant can be created with one (enforced by the code). Therefore, when the Profile is persisted by JPA I receive this error:
2012-10-17 16:48:30,337 WARN [AWT-EventQueue-0] - -- UnitOfWork(24490984)
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: [SQLITE_CONSTRAINT] Abort due to constraint violation (column ID is not unique)
Error Code: 0
Call: INSERT INTO PROFILE (ID, NAME, CONVERGENCECRITERION, INITIALENRICHMENT, POWER, POWEREXPONENT, TRAMPBURNUP, XE133CLEANUPRATE) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
bind => [8 parameters bound]
I found (after considerable digging and trying various solutions) a link that suggested I remove the 'not null' from my primary key ID in Profile. Since the post was not 100% clear to me I am wondering if anyone else has heard of this and/or if it makes sense.
Here is a piece of the post, and by the way, it solved my issue!
While checking every thing from scratch found that thee where condition enforced on the column of table NOT NULL, like id was PRIMAY KEY AUTO INCREMENT NOT NULL. This was the cause of the issue , as I wanted the SQLite to generate id for me but also had said you require an id field for an insertion to complete. The same problem was with some other column.
So I decided to keep some guideline for dealing with SQLite and iPhone connections.
1. Never create primary key column of table with both the restrictions ON (TUE) , that is AUTO INCREMENT NOT NULL. This rule can ignored, if you want to manage your own primary key creation.
This only seems to happen for the integer-type PK ID fields in my case- probably b/c of the @GeneratedValue annotation.
Powered by FUDForum
. Page generated in 0.06652 seconds