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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> wrote:
> >
> > Hi,
> >
> > Have you told OpenJPA about your foreign key constraints?
> >
> > -Patrick
> >
> > On Dec 28, 2007 7:15 PM, Miroslav Nachev <[EMAIL PROTECTED]> 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
> >
>
>