You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Alexander Saint Croix <sa...@gmail.com> on 2008/01/11 21:06:12 UTC

Re: Cascade on Delete is not working

Miro,

I am having troubles which appear identical to yours.

I have a class "Foo" that has a ManyToOne bidirectional association with a
class "Bar", controlled by "Bar".  The relationship requires that if a Foo
bean is deleted, any Bar bean it maps to must also be deleted, but not vice
versa.  However, my tests verify that this is not happening.  I presume I'm
doing something wrong.

My annotations look like this:


@Entity
> public class Foo {
>
>     @Id
>     @GeneratedValue
>     private long ID;
>
>     @OneToMany(cascade=CascadeType.ALL, mappedBy="foo")
>     private Set<Bar> bars = new HashSet<Bar> ();
>
> ...
> }
>
>
> @Entity
> public class Bar {
>
>     @Id
>     @GeneratedValue
>     private long ID;
>
>     @ManyToOne(cascade = {
>             CascadeType.PERSIST,
>             CascadeType.REFRESH ,
>             CascadeType.MERGE})
>     private Foo foo;
>
> ...
> }
>

What I intend to do in the immediate future is to create a manager object
that will take care of deletions for me, so when I delete an instance of
foo, referencing instances of bar are deleted, and any references to bar in
other entities (such as class "funk", for example), are likewise removed.

This removal of references in collections seems slightly different than what
is described in the cascade remove literature.  Cascading remove seems to
focus on wiping out entire rows instead of clearing fk references, so I
recognize in my setup I need to do a little helping work.

This isn't as great as cascading, but OpenJPA's already doing a heckuva job
for me--I'm happy to meet them in the middle.

I don't know if this helps you solve your problem, but at least you're not
alone ;-)

Regards,
--
Alex







On Dec 30, 2007 2:39 PM, Miroslav Nachev <mi...@space-comm.com> wrote:

> Hi Patrick,
>
> Can you give me some ideas and/or suggestions how to delete main object
> and
> all related records with foreign keys? In my opinion the option Cascade
> ALL
> is for that reason. Is it true?
>
>
> Regards,
> Miro.
>
> On 12/28/07, Miroslav Nachev <mi...@space-comm.com> wrote:
> >
> > CREATE TABLE test_table_1
> > (
> >   test_id numeric(36) NOT NULL,
> >   is_deleted boolean NOT NULL DEFAULT false,
> >   CONSTRAINT test_table_1_pkey PRIMARY KEY (test_id)
> > )
> >
> > CREATE TABLE test_table_2
> > (
> >   tt2_id numeric(36) NOT NULL,
> >   tt1_id numeric(36) NOT NULL,
> >   CONSTRAINT pk_tt2 PRIMARY KEY (tt2_id),
> >   CONSTRAINT fk_tt2_tt1_id FOREIGN KEY (tt1_id)
> >       REFERENCES test_table_1 (test_id)
> > )
> >
> > CREATE TABLE test_table_3
> > (
> >   tt3_id numeric(36) NOT NULL,
> >   text_data character varying(255),
> >   CONSTRAINT pk_tt3 PRIMARY KEY (tt3_id),
> >   CONSTRAINT fk_tt3_tt1_id FOREIGN KEY (tt3_id)
> >       REFERENCES test_table_1 (test_id)
> > )
> >
> >
> > M.
> >
> >
> > On 12/28/07, Miroslav Nachev <mi...@space-comm.com> wrote:
> > >
> > > Yes. I would like when delete one record from TestTable1 all records
> in
> > > TestTable2 and TestTable3 which refer to this record in TetTable1 to
> be
> > > cascade deleted. In TestTable2 and TestTable3 this records refer to
> > > TestTable1 as Foreign Key.
> > >
> > >
> > > Miro.
> > >
> > > On 12/28/07, Patrick Linskey < plinskey@gmail.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > Have you told OpenJPA about your foreign key constraints?
> > > >
> > > > -Patrick
> > > >
> > > > On Dec 28, 2007 7:15 PM, Miroslav Nachev < miro@space-comm.com>
> wrote:
> > > > > Hi,
> > > > >
> > > > > I have 3 tables:
> > > > > - TestTable1 (test_table_1) which is master for the rest tables
> > > > >    @Id
> > > > >    @Column(name = "test_id", nullable = false)
> > > > >    private BigDecimal testId;
> > > > >    @Column(name = "is_deleted", nullable = false)
> > > > >    private boolean deleted;
> > > > >    @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER,
> > > > mappedBy =
> > > > > "tt1Id")
> > > > >    private Collection<TestTable2> testTable2Collection;
> > > > >    @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER,
> > > > mappedBy =
> > > > > "testTable1")
> > > > >    private TestTable3 testTable3;
> > > > >
> > > > > - TestTable2 (test_table_2)
> > > > >    @Id
> > > > >    @Column(name = "tt2_id", nullable = false)
> > > > >    private BigDecimal tt2Id;
> > > > >    @JoinColumn(name = "tt1_id", referencedColumnName = "test_id")
> > > > >    @ManyToOne
> > > > >    private TestTable1 tt1Id;
> > > > > - TestTable3 (test_table_3)
> > > > >    @Id
> > > > >    @Column(name = "tt3_id", nullable = false)
> > > > >    private BigDecimal tt3Id;
> > > > >    @Column(name = "text_data")
> > > > >    private String textData;
> > > > >    @JoinColumn(name = "tt3_id", referencedColumnName = "test_id",
> > > > > insertable = false, updatable = false)
> > > > >    @OneToOne
> > > > >    private TestTable1 testTable1;
> > > > >
> > > > > When I try to delete some record from TestTable1 I have the
> > > > following
> > > > > exception:
> > > > > Caused by: < openjpa-1.1.0-SNAPSHOT-r420667:606946M nonfatal
> general
> > > > error>
> > > > > org.apache.openjpa.persistence.PersistenceException: ERROR: update
> > > > or delete
> > > > > on table "test_table_1" violates foreign key constraint
> > > > "fk_tt2_tt1_id" on
> > > > > table "test_table_2"
> > > > >  Detail: Key (test_id)=(1198864038296) is still referenced from
> > > > table
> > > > > "test_table_2". {prepstmnt 25877218 DELETE FROM test_table_1 WHERE
> > > > test_id =
> > > > > ? [params=(BigDecimal) 1198864038296]} [code=0, state=23503]
> > > > > ...
> > > > >
> > > > >
> > > > > Where did I go wrong? Why I can not delete record from master
> table
> > > > and all
> > > > > records from referenced tables to be cascade deleted?
> > > > >
> > > > >
> > > > > Miro.
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > Patrick Linskey
> > > > 202 669 5907
> > > >
> > >
> > >
> >
>