Friday, 15 January 2010

java - Play + Ebean + JPA: Cascade a delete on a OneToOne mapping -


This is related, but the example below is small, so I have been asked another question.

I have two bodies, A and B, in each one-one relationship. For A, B is optional, and every seed should have A. I want to remove A to B. This is my first attempt: @Entiree Public Class A's Expansion Model {@ ID Private Long ID; @OneToOne (optional = true, mapped = "A", cascade = cascade type; removes, orphan removals = true) private BB; } @Entity Public Square B Extended Models {@Id Private Long ID; @OneToOne (optional = false) Private AA; }

However, it seems that EBI ignores "alternative" annotations, because when I execute a search for the saved with ID1, the following SQL executes Is:

  select t0.id c0, t1.id c1, join t1, b1 on t1. A_id = t0.id where t0.id = 1   

In other words, this is an internal rather than a left join, which is not a failure because I have no associated B Have done I have tried various combinations of @JoinColumn etc. for any benefit. I found that somewhat satisfactory solution, to model A-to-B as a "fake" one-to-many relationship, is there a better solution? Is this a bug or is it a known / eminent limit of Ebine?

A from and deleted the B class in mapped = "b" .
Now the code looks like this:
  @Entity public class A model extension {@Id Private Long ID; @OneToOne (optional = true, cascade = cascade type. Removes, orphan removals = true) private BB; ...} @Entity Public Class B Extend Model {@Id Private Long ID; @OneToOne (optional = false, mapped = "b") Private AA; The name of the private string; ...}   

I added the name field to B class to make this exam more interesting.

My test medhod:

  @Test public zero abTest () {FakeApplication app = helpers.fakeApplication (Helpers.inMemoryDatabase ()); Helpers.start (app); AA = new A (); BB = new B (); A.setId (1L); B.setId (2L); A.setB (b); B.setA (a); B.setName ("bbb"); Ebean.save (b); Ebean.save (a); An FA = EB.N.Find (A class, 1l); System.out.println ("a.id:" + fa.getId ()); System.out.println ("a.b.id:" + fa.getB ()); System.out.println ("a.b.name:" + fa.getB (). GetName ()); A1 = new A (); A1.setId (3l); Ebean.save (A1); A fa1 = Ebean.find (Eccles, 3L); System.out.println ("a1.id:" + fa1.getId ()); System.out.println ("a1.b.id:" + fa1.getB ()); BFB = Ebean.find (B Class, 2L); System.out.println ("b.id:" + fb.getId ()); System.out.println ("b.name:" + fb.getName ()); System.out.println ("b.a.id:" + fb.getA (.) GetId ()); }   

And the result of this exam is:

  [debug] cjbPreparedStatementHandle - b (ID, name) Enter values ​​(2, 'BBB') [Debug] CZBPPrttifiedStatementHandle- Enter one (id, b_id) values ​​(1,2) [debug] CJBPPritedatedStatementhandle-T0.IDC0, T0.B_ID C1 Choose from T, where T.ID = 1Ad: 1 Abid: models.B@2 [debug] cjbPreparedStatementHandle - t0.id c0, t0.name c1, t1.id c2 to b t0 Select t1.b_id = t0.id to join in a t1 where t0. Id = 2 abname: bbb [debug] cjbPreparedStatementHandle - Select a (id, b_id) values ​​(3, '[SQL type type -5]') [debug] CJBPPreatedStatement Handle - T0.ID C, T. Select a T to b_id c1 where t0.id = 3 a1.id: 3 a1.b.id: null [debug] cjbPreparedStatementHandle - t0.id c0, t0.name c1, t1.id c2 b t0 to the left T1.b_id = t0.id on external participant T1 where t0.id = 2 b.id: 2b.name: bbb baid: 1   

so this code works well if Ab is null or not, as we can see in the log instead of join

No comments:

Post a Comment