Perhaps I'm over-simplifying this by only giving you a small snippet of code (and I'll post more if that is the case) but I figure, initially, less is better:
I have an entity of type Asset which has a field of type Location, which is also an entity. When I set the Location of an Asset I must also set the Location of its children.
Location location = asset.getLocation();
em.merge(location);
em.flush();
childAsset.setLocation(asset.getLocation());
em.flush();
When I run flush(), I get the following exception:
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (SWRADMIN.LOCATION_PK) violated
My question is...why is this Location object even trying to be persisted? All I'm doing is setting a variable in an entity.
This was working fine before, but we just switched to using Eclipselink and Java EE 6 and this problem popped up.
Solution?: I used the idea of "detaching" from below and made the following change:
Location location = asset.getLocation();
em.detach(childAsset);
childAsset.setLocation(asset.getLocation());
em.merge();
em.flush();
and it worked! I'm confused as to why, though...you would think the auto-syncing would do the same thing.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…