PersistenceException upon flush() - constraint violation [message #949196] |
Thu, 18 October 2012 15:55  |
Eclipse User |
|
|
|
Hello.
My question at the end is: I found a solution - does it seem like an acceptable solution?
Issue:
Profile.java has the following:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
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]
Query: InsertObjectQuery(Westinghouse)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:851)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:913)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:594)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:537)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1800)
at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:286)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:342)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:162)
at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:177)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:471)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(Datab
.... etc.
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.
Feedback appreciated.
This only seems to happen for the integer-type PK ID fields in my case- probably b/c of the @GeneratedValue annotation.
|
|
|
|
|
|
|
|
|
Re: PersistenceException upon flush() - constraint violation [message #957679 is a reply to message #956755] |
Thu, 25 October 2012 08:05  |
Eclipse User |
|
|
|
I was wrong- has nothing to do with the EclipseLink version.
I think I found the issue. I was calling em.clear() after an em.remove() which is why my profile was detached. When I remove the em.clear() call, the exceptions are gone. I believe Chris had it correct from the start and I just took a long time to figure out how it was all working in my code.
The Entity Object Life Cycle diagram I've attached helped me a lot too.
Sorry for all the posts.
|
|
|
Powered by
FUDForum. Page generated in 0.05566 seconds