Hibernate的OneToOne注释是否可以跨多个联接表工作

时间:2018-08-22 20:23:11

标签: hibernate one-to-one

我有三个实体用户,公司和地址。每个用户都有一个地址,每个公司都有一个地址。这些实体中的每一个都有一个单独的表。我希望地址不可能同时属于用户和公司。

在用户和公司联接列上使用@OneToOne注释是否可行?

SQL> with test (s_id, sg_id, r_cost, w_cost, av_id, v_id) as
  2  (select 123, 100, 0.5, 1, 1, 333 from dual union all
  3   select 123, 105, 0.75, 0, 2, 333 from dual union all
  4   select 123, 330, 1, null, 3, 888 from dual
  5  )
  6  select t.s_id,
  7         t.v_id,
  8         max(nvl(t.w_cost, t.r_cost)) w_cost,
  9         (select t1.av_id
 10          from test t1
 11          where nvl(w_cost, r_cost) = (select max(nvl(t2.w_cost, t2.r_cost))
 12                                       from test t2)
 13            and t1.s_id = t.s_id
 14            and t1.v_id = t.v_id
 15         ) av_id
 16  from test t
 17  group by t.s_id, t.v_id
 18  order by 1, 2;

      S_ID       V_ID     W_COST      AV_ID
---------- ---------- ---------- ----------
       123        333          1          1
       123        888          1          3

SQL>

0 个答案:

没有答案