JPA and Spring data: why did I get delete error -



JPA and Spring data: why did I get delete error -

i have 3 classes (a has many b, has many c) :

class { @onetomany (mappedby="a", fetch = fetchtype.lazy, cascade=cascadetype.all) public list<b> bs; public list<b> getbs() { homecoming bs; } public void setbs(list<b> bs) { this.bs = bs; } } class b { @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "a") private a; public geta() { homecoming a; } public void seta(a a) { this.a = a; } @onetomany (mappedby="b", fetch = fetchtype.lazy, cascade=cascadetype.all) public list<c> cs; public list<c> getcs() { homecoming cs; } public void setcs(list<c> cs) { this.cs = cs; } } class c { @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "b") private b b; public b getb() { homecoming b; } public void setb(b b) { this.b = b; } }

i utilize spring info handle delete. here able successfully:

if delete single record b, record deleted , associated c records automatically deleted too.

if delete single record a, record deleted , associated b/c records automatically deleted too.

however, if want delete a's b records (without delete record) via following:

public void deletebya(a a) { //em entitymanager query q = em.createnamedquery("delete b = :a"); q.setparameter("a", a); q.executeupdate(); }

i next error:

the delete statement conflicted reference constraint "fk_1pbkk0g3b9vnx7apvisddsh7e". conflict occurred in database "mydb", table "dbo.c", column 'b'.

is there obvious place in above did wrong? possible problem?

thanks input!!!

the problem delete queries don't take cascade annotations account. they're low-level queries, translated sql , executed, bypassing session (i.e. if delete entity in session, won't marked deleted in session) , cascade mechanisms.

so, either bs , delete them calling em.remove(), or sql: delete cs explicitely first, delete bs, using delete queries. 1 time again, read restrictions: delete queries can't utilize joins (unless joins in subselect).

here's relevant documentation.

jpa spring-data

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -