Removing unidirectional or bidirectional entity

64 views
Skip to first unread message

Daniel Guryca

unread,
Feb 20, 2010, 5:25:58 PM2/20/10
to play-fr...@googlegroups.com
Hi,

After spending some time with trying to delete my entity which is pretty heavy I started again with something very simple since I want to understand it.

Unidirectional OneToOne:

@Entity
public class Face extends Model {
@Column(nullable = false)
public String name;

@OneToOne(cascade = CascadeType.ALL)
public Nose nose;
}

@Entity
public class Nose extends Model {
@Column(nullable = false)
public String title;
@PreRemove
public void deleteNose() {
                Face face = Face.find("nose=?", this).first();
face.nose = null;
face.save();
}
}



Now tell me how do you delete:
1) Only nose entity
2) Face entity and also it's nose entity

With existing mappings I can do for 1) case:
face.nose.delete()   ... and it works.

But it's understandable that it will not work for 2) case:
face.delete()

I can rewrite my @PreRemove so that it works for 2) too but then it will not work for 1) ... so no universal solution here.
Is there any simpler solution ... it seems to be too complicated to do such a trivial task like entity removal.
How do you remove entities ?

It's similar for bidirectional mappings too .. so I can imagine very similar examples and troubles I will have to solve.

Thank you
Daniel

Guillaume Bort

unread,
Feb 21, 2010, 6:28:34 AM2/21/10
to play-fr...@googlegroups.com
Cascase delete is sometimes very hard to manage. You effectively have
to detach relation manually. But doing it in the the JPA event
listener is sometimes too late.

It is more difficult to manage in play when you extends Model because
the save is explicit and database instruction are flushed directly (it
solves a lot of other problems to manage save/delete explicitly).

So best way is often to override the delete() method.

So here my solution for your 2 uses case:

---

@Entity
public class Face extends Model {

public String name;

@OneToOne(cascade = CascadeType.ALL)
public Nose nose;

}

---

@Entity
public class Nose extends Model {

@Column(nullable = false)
public String title;

public Nose delete() {
Face face = Face.find("nose", this).first();
face.nose = null;
face.save();
return super.delete();
}

}

---

@Test
public void case1() {
Face face = Face.find("byName", "Guillaume").first();
face.nose.delete();
assertEquals(0, Nose.count());
assertEquals(1, Face.count());
}

@Test
public void case2() {
Face face = Face.find("byName", "Guillaume").first();
face.delete();
assertEquals(0, Nose.count());
assertEquals(0, Face.count());
}

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.
>

Daniel Guryca

unread,
Feb 22, 2010, 2:24:25 PM2/22/10
to play-fr...@googlegroups.com
Hey,

Guillaume thanks !

For entities which are defined in our modules and are extended it can really become difficult.

Nevermind ... it works.

Daniel
Reply all
Reply to author
Forward
0 new messages