You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Srinivasan Krishnamoorthy <sr...@live.com> on 2009/10/14 16:24:44 UTC

Maintaining foreign key relation in Unidirectional ManyToOne

Hi,
 I have a manyToOne relation which is unidirectional.
For example, say Student and College. Student has manyToOne relation with
college. But College do not have OneToMany with Student. 
Here, if I delete College, I want all the students belonging to it be
deleted.
Can you please let me know a way to do this with JPA?

Best Regards,
Srinivasan Krishnamoorthy.

-- 
View this message in context: http://n2.nabble.com/Maintaining-foreign-key-relation-in-Unidirectional-ManyToOne-tp3823080p3823080.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Maintaining foreign key relation in Unidirectional ManyToOne

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Oct 14, 2009 at 11:12 PM, Srinivasan Krishnamoorthy <
srinivasan.krish@live.com> wrote:

>
> Hi Daryl,
>  Thanks for the immediate response.. I have one question..
> If I use database cascade to do this, will it throw the cache out
> of sync, If I had cached students? I was thinking how will JPA
> know that these students are now deleted.
>

That's an excellent question. I'm not using the L2 cache. I don't think
using only a DB cascade is the right way to go - just an alternative to the
others. Why not add the relation to College?

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Maintaining foreign key relation in Unidirectional ManyToOne

Posted by Srinivasan Krishnamoorthy <sr...@live.com>.
Hi Daryl,
 Thanks for the immediate response.. I have one question.. 
If I use database cascade to do this, will it throw the cache out 
of sync, If I had cached students? I was thinking how will JPA
know that these students are now deleted.

Best Regards,
Srinivasan Krishnamoorthy.



Daryl Stultz wrote:
> 
> On Wed, Oct 14, 2009 at 10:24 AM, Srinivasan Krishnamoorthy <
> srinivasan.krish@live.com> wrote:
> 
>>
>> Hi,
>>  I have a manyToOne relation which is unidirectional.
>> For example, say Student and College. Student has manyToOne relation with
>> college. But College do not have OneToMany with Student.
>> Here, if I delete College, I want all the students belonging to it be
>> deleted.
>> Can you please let me know a way to do this with JPA?
>>
>>
> If you want it to be automatic you need to put in the relation
> college.students with CascadeType.DELETE. You can make the getters/setters
> private if you don't want the collection loaded. The only other automatic
> way would be cascade delete on the foreign key in the database. If you
> don't
> need it automatic you can do the delete yourself. (delete from Student s
> where s.college = :college)
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 

-- 
View this message in context: http://n2.nabble.com/Maintaining-foreign-key-relation-in-Unidirectional-ManyToOne-tp3823080p3827043.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Maintaining foreign key relation in Unidirectional ManyToOne

Posted by Daryl Stultz <da...@6degrees.com>.
On Wed, Oct 14, 2009 at 10:24 AM, Srinivasan Krishnamoorthy <
srinivasan.krish@live.com> wrote:

>
> Hi,
>  I have a manyToOne relation which is unidirectional.
> For example, say Student and College. Student has manyToOne relation with
> college. But College do not have OneToMany with Student.
> Here, if I delete College, I want all the students belonging to it be
> deleted.
> Can you please let me know a way to do this with JPA?
>
>
If you want it to be automatic you need to put in the relation
college.students with CascadeType.DELETE. You can make the getters/setters
private if you don't want the collection loaded. The only other automatic
way would be cascade delete on the foreign key in the database. If you don't
need it automatic you can do the delete yourself. (delete from Student s
where s.college = :college)

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com