回答

收藏

Hibernate Create Criteria两次连接同一表-尝试了2种方法,但有2个差异错误

技术问答 技术问答 61 人阅读 | 0 人回复 | 2023-09-13

我想为以下本机sql创建创建条件。6 a, J9 `- i; b
不幸的是,我两次使用createCriteria时遇到重复的关联路径错误。当我尝试使用Restrictions.sqlRestriction时。它无法提供我想要的SQL。/ x0 j: T8 w, P- a2 V( I( @4 ], u
尝试1:创建条件-重复的关联路径
; N/ s' c% w; B, t6 E4 G1 y( U Criteria criteria = getSession().createCriteria( Company.class );
. n8 R, S5 Y$ h" o, N7 n4 n  r                 criteria.createAlias( "customerCategories", "c1" );  X/ L0 k7 b6 ?$ n6 y  M9 _' s* ]1 R
                 criteria.add( Restrictions.in( "c1.customerCategory.customerCategoryId",
' n+ q* ]5 n9 y                         company.getBaseCustomerCategoryId() ) );
* V4 }7 }2 E* L+ B/ z& C                 criteria.createAlias( "customerCategories", "c2" );6 J5 Y/ f) P, `4 D3 m' Q- C2 o9 i
                 criteria.add( Restrictions.in( "c2.customerCategory.customerCategoryId",2 a6 \' q/ N2 L1 Y& Y
                         company.getPromoCustomerCategoryId() ) );
! z% R! p0 ^5 {4 L尝试2:创建SQL限制-ORA-00920:由于“ where”,关系运算符无效
8 v& k6 n  R# E  U1 S5 S' G/ f$ ~! @  Criteria criteria = getSession().createCriteria( Company.class );& q, R$ D+ [  M" l9 j; w* U
                 criteria.add( Restrictions.sqlRestriction(
0 N* o- j/ X: u# D5 Z" c3 j                         "INNER JOIN Company_Customercategory a on {alias}.companyId = a.companyId and a.CUSTOMERCATEGORYID = ?",
6 w" m4 u% b% ^7 w* V                         company.getBaseCustomerCategoryId(), LongType.INSTANCE ) );9 H* C3 M6 z# a
                 criteria.add( Restrictions.sqlRestriction(
( S6 n) @) \* v! O; B: \* H                         "1=1 INNER JOIN Company_Customercategory b on {alias}.companyId = b.companyId" ]& T+ E( L3 p% l. S# c2 V
and b.CUSTOMERCATEGORYID = ?",
8 v/ `! M8 t5 D' M6 q- n4 p; y                         company.getPromoCustomerCategoryId(), LongType.INSTANCE) );- z# i3 r0 w/ |$ z, c9 Z
结果错误/ d" ?  ^0 v4 c) _& C
select this_.* from Companies this_ where
# u6 Q7 h! B+ |0 l# t  INNER JOIN Company_Customercategory a
- I# }2 O8 F; f, G: ~, L) j  on this_.companyId = a.companyId : f/ g" \; ?0 P2 g  b8 H2 o
  and a.CUSTOMERCATEGORYID = 1
) d& Q7 H1 U2 k1 b, J  and 1=1 INNER JOIN Company_Customercategory b
* y3 Y; e+ z& Z* o: B3 i' t  on this_.companyId = b.companyId
/ t# R! o$ X# P* u' J  and b.CUSTOMERCATEGORYID = 6% X% D7 J, D- R" j9 ^; O1 _% g
预期的SQL
% _0 L' N, n/ G3 pselect * from companies c
. ?5 D( O' T% G" F% q: F  inner join Company_Customercategory a% e. M$ E4 b, U! ~  S- l
  on c.companyId = a.companyId% k# u! ^( o7 ?5 t" W
  and a.CUSTOMERCATEGORYID = 17 m9 u! ?6 ?8 y- J) T. g8 L1 n
  inner JOIN Company_Customercategory b0 d8 g5 ]9 c: J7 Z6 ?6 z
  on a.companyId = b.companyId
/ g4 `. A& v! x5 l1 o) v  and b.CUSTOMERCATEGORYID = 6" M# X+ j, o, p6 i
感谢你的帮助。谢谢。5 K$ y3 e; B' N% G" y" I
               
6 o+ }% b( }- X! c解决方案:5 p! M9 ^4 {5 n7 Q' j/ l9 |$ {
                5 T$ l$ V: c1 Z+ K: Y

' i5 o7 e# d2 K3 a$ m* ^( n1 u8 C: J% ?4 ~  @3 A) T+ N, ~) }7 i
                关于2005年打开的问题,还有一个旧的Hibernate错误HHH-879org.hibernate.QueryException:
# ~8 R, f8 j/ S' t6 Z1 N1 {duplicate association path仍在打开…
  N3 ]1 B- X- W* x" z2 P4 k& W+ ^其他问题已解决,但未解决HHH-7882
. H# F. p) ^- b$ z/ a) o因此,选项1)不太适合。1 _0 |$ O! t' X, D
但是,在上述错误的意见的有用 的解决方法 是使用提及exists
& y" c+ v2 }4 v' K因此,请使用两次sqlRestriction,exists并使用一个相关的子查询来过滤属性类别。您将获得唯一的 企业 连接到这两个类别。2 f  d' L, a0 _) P2 N. D1 ~
crit.add( Restrictions.sqlRestriction(
; G: C) x$ i9 }9 ?  Z1 H1 z  "exists (select null from Company_Customercategory a where {alias}.company_Id = a.company_Id and a.CUSTOMERCATEGORYID = ?)",& I7 g# m7 i& {# v6 |$ U9 n+ M$ t
  1, IntegerType.INSTANCE ) );1 R0 A- }! k* t2 S9 E. }
crit.add( Restrictions.sqlRestriction(
' B/ p( _/ |% ^3 ]  a  "exists (select null from Company_Customercategory a where {alias}.company_Id = a.company_Id and a.CUSTOMERCATEGORYID = ?)",& S5 F* _* }) G" V% U3 F8 v
  6, IntegerType.INSTANCE ) );, z& {) H/ l4 }  c2 f" p9 w, D7 X
这导致以下查询提供正确的结果! }3 `& R* I% T3 H  p( r6 F
select this_.COMPANY_ID as COMPANY_ID1_2_0_, this_.COMPANY_NAME as COMPANY_NAME2_2_0_ * a( l; }+ r) j: N" b+ W8 D* I4 r! l/ W
from COMPANIES this_ 0 k, w" _% W# X
where exists (select null from Company_Customercategory a 4 s! P+ m2 _1 M7 \9 \8 h! ?
              where this_.company_Id = a.company_Id and a.CUSTOMERCATEGORYID =  ?) and
/ F% `7 m5 i, o5 s      exists (select null from Company_Customercategory a 5 g; D+ R: I+ R0 C
              where this_.company_Id = a.company_Id and a.CUSTOMERCATEGORYID = ?)
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则