glassfish - JPA bidirectional relations: structure in Database -


i'm starting using jpa in netbeans glassfish server + derby database, , have doubts on behavior. did following experiment: defined entity "user" property

@onetomany(cascade=all, mappedby="user")     public list<thing> getthings() {     return things; } 

and entity "thing" property

@manytoone public cook4user getuser() {     return user; } 

then persisted 1 user , added 1 "thing" it. right, can see 2 tables "user" , "thing" 1 entry each, second table having foreign key indicating user id. removed element "thing" table, ran select statement recover user, called getthings() method on and... still found element had removed thing table! how possible? stored? can't see in db! clearing things me.

edit: tried isolate lines of code produce issue.

@persistencecontext private entitymanager em;  user user = new user(); em.persist(user); thing thing = new thing(); em.persist(thing); user.getthings().add(thing); em.remove(thing); user = em.find(user.class, userid); logger.log(level.info, "user still contains {0} things", user.getthings().size()); \\thing still there! 

in form of junit test springrunner , hibernate jpa implementation:

@runwith(springjunit4classrunner.class) @contextconfiguration("classpath:spring-context.xml") public class testthings {      private final logger log = loggerfactory.getlogger(testthings.class);      @persistencecontext     private entitymanager em;      @test     @transactional     public void does_not_remove_thing() {         cook4user user = new cook4user();         em.persist(user);          thing thing = new thing();         em.persist(thing);         user.getthings().add(thing);          user = em.find(cook4user.class, user.getid());         user.getthings().foreach((t) -> log.info("1 >> users thing: {}", t.getid()));          em.remove(thing);         em.flush();          user = em.find(cook4user.class, user.getid());         user.getthings().foreach((t) -> log.info("2 >> users thing: {}", t.getid()));          assertthat(user.getthings()).isempty();     }      @test     @transactional     public void removes_thing_when_removed_from_owning_side() {         cook4user user = new cook4user();         em.persist(user);          thing thing = new thing();         em.persist(thing);         user.getthings().add(thing);          user = em.find(cook4user.class, user.getid());         user.getthings().foreach((t) -> log.info("1 >> users thing: {}", t.getid()));          user.getthings().remove(thing);          user = em.find(cook4user.class, user.getid());         user.getthings().foreach((t) -> log.info("2 >> users thing: {}", t.getid()));          assertthat(user.getthings()).isempty();     } } 

the first test does_not_remove_thing per question , fails have experienced, output of test hibernate.show_sql logging set true:

hibernate:      insert              cook4user         (id)      values         (null) hibernate:      insert              thing         (id, user_id)      values         (null, ?) [main] info  testthings - 1 >> users thing: 1 [main] info  testthings - 2 >> users thing: 1 java.lang.assertionerror: expecting empty was:<[x.thing@276]> 

the second test removes_thing_when_removed_from_owning_side passes output:

hibernate:      insert              cook4user         (id)      values         (null) hibernate:      insert              thing         (id, user_id)      values         (null, ?) [main] info  testthings - 1 >> users thing: 1 

so looks removing thing owning side of relationship (he he) way go.

although, must honest, i'm not sure why works , way not. understand if removed thing detached entity not case. also, expecting delete query after calling em.remove(thing) thing somewhere nothing (i added em.flush() try force that).

maybe else can shed light on finer mechanics of what's going on here?


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -