FK object which is a part of PK in the parent is also persisted once the the Parent is persisted although no cascade was set on ManyToOne.
Please let me know if this is normal and not a bug:
This is the spec for persisting and entity: 3.2.2 Persisting an Entity Instance A new entity instance becomes both managed and persistent by invoking the persist method on it or by cascading the persist operation. The semantics of the persist operation, applied to an entity X are as follows: • If X is a new entity, it becomes managed. The entity X will be entered into the database at or before transaction commit or as a result of the flush operation. • If X is a preexisting managed entity, it is ignored by the persist operation. However, the persist operation is cascaded to entities referenced by X, if the relationships from X to these other entities are annotated with the cascade=PERSIST or cascade=ALL annotation element value or specified with the equivalent XML descriptor element. • If X is a removed entity, it becomes managed. • If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time. • For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the cascade element value cascade=PERSIST or cascade=ALL, the persist operation is applied to Y.
Below are the entities: Binding has ManyToOne uni-directional towards Application and ServerHost. Being ServerHost and Application are already in the DB. When a new Binding is created (by the help of ServerHost and Application entites) and persisted, hibernate tries to insert the same Application which is already in the DB hence Violation of PRIMARY KEY constraint 'PK_Application'. Cannot insert duplicate key in object error.
|