Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] cardinality does not correspond

Thanks, James.  I guess that should've been apparent, but I'm an object
guy and was thinking in terms of object instances, not db rows.  Got it now.

I have a conceptual question now: am I using the annotations below
properly?  I have a working one to many mapping with a delete of the
parent cascading properly to the children (i.e. when I remove a quiz
reference from quizDAOs and commit the user entity, the quiz gets
deleted from the db.)  However it requires both the @PrivateOwned and
the @ManyToOne(cascade = CascadeType.ALL) annotations as follow. 

Why is the cascade element needed on the @ManyToOne annotation? 
Shouldn't the cascade specified on the owning side manage the quiz
delete?  If I remove either the @PrivateOwned or the cascade element of
the @ManyToOne, I get a foreign key constraint violation.  However in
all 3 situations the db schema looks the same with a foreign key on the
quizdao table pointing to userdao.

public class UserDAO {
    @PrivateOwned
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userDAO")
    @MapKey(name = "quizTag")
    private Map<String, QuizDAO> quizDAOs = new HashMap<String, QuizDAO>();
...}

public class QuizDAO {
    @ManyToOne(cascade = CascadeType.ALL)
    private UserDAO userDAO;
...}

I read what little I could find about @PrivateOwned here
(http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)), and
it seems like that tag is required for the my situation: "If you remove
the reference to a target from a source, then delete the target."

Error when no @PrivateOwned specified:
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot delete or update a parent row: a foreign key constraint fails
(`test/quizdao`, CONSTRAINT `FK_QUIZDAO_USERDAO_ID` FOREIGN KEY
(`USERDAO_ID`) REFERENCES `userdao` (`ID`))
Error Code: 1451
Call: DELETE FROM USERDAO WHERE (ID = ?)
    bind => [49]
Query: DeleteObjectQuery(com.planityou.logic.user.UserDAO@1c794cc)

Error when no cascade element on @ManyToOne specified:
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot delete or update a parent row: a foreign key constraint fails
(`test/quizdao`, CONSTRAINT `FK_QUIZDAO_USERDAO_ID` FOREIGN KEY
(`USERDAO_ID`) REFERENCES `userdao` (`ID`))
Error Code: 1451
Call: DELETE FROM USERDAO WHERE (ID = ?)
    bind => [49]
Query: DeleteObjectQuery(com.planityou.logic.user.UserDAO@1ed27e4)

Am new to JPA and dbs and want to make sure I'm using the annotations
properly.  Thanks.


Back to the top