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

You should not need CascadeType.ALL with @PrivateOwned.  You should not have
CascadeType.ALL on your Quiz, deleted the User should cause the Quiz to be
deleted, not the other way around.

@PrivateOwned is an EclipseLink JPA extension, in plain JPA you would need
to call remove() on the Quiz when you remove it from the User.



otismo wrote:
> 
> 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.
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/cardinality-does-not-correspond-tp21384261p21437784.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top