1515 */
1616package com .navercorp .spring .data .jdbc .plus .sql .convert ;
1717
18+ import javax .annotation .Nullable ;
19+
20+ import org .springframework .data .mapping .PersistentPropertyPath ;
1821import org .springframework .data .relational .core .mapping .PersistentPropertyPathExtension ;
1922import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
23+ import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
2024import org .springframework .data .relational .core .sql .Column ;
2125import org .springframework .data .relational .core .sql .SqlIdentifier ;
2226import org .springframework .data .relational .core .sql .Table ;
27+ import org .springframework .util .Assert ;
28+
29+ import com .navercorp .spring .data .jdbc .plus .sql .annotation .SqlTableAlias ;
2330
2431/**
2532 * Utility to get from path to SQL DSL elements.
@@ -60,8 +67,7 @@ public Table getTable() {
6067
6168 @ Override
6269 public Table getTable (PersistentPropertyPathExtension path ) {
63-
64- SqlIdentifier tableAlias = path .getTableAlias ();
70+ SqlIdentifier tableAlias = this .getTableAlias (path );
6571 Table table = Table .create (path .getTableName ());
6672 return tableAlias == null ? table : table .as (tableAlias );
6773 }
@@ -75,4 +81,52 @@ public Column getColumn(PersistentPropertyPathExtension path) {
7581 public Column getReverseColumn (PersistentPropertyPathExtension path ) {
7682 return getTable (path ).column (path .getReverseColumnName ()).as (path .getReverseColumnNameAlias ());
7783 }
84+
85+ // Refer from PersistentPropertyPathExtension#getTableAlias
86+ @ Nullable
87+ private SqlIdentifier getTableAlias (PersistentPropertyPathExtension path ) {
88+ PersistentPropertyPathExtension tableOwner = getTableOwningAncestor (path );
89+ if (tableOwner .getLength () > 0 ) { // path != null
90+ return this .assembleTableAlias (tableOwner );
91+ }
92+
93+ // path == null : root
94+ SqlTableAlias sqlTableAlias = tableOwner .getLeafEntity ().findAnnotation (SqlTableAlias .class );
95+ if (sqlTableAlias != null ) {
96+ return this .table .as (sqlTableAlias .value ()).getReferenceName ();
97+ }
98+
99+ return null ;
100+ }
101+
102+ private PersistentPropertyPathExtension getTableOwningAncestor (PersistentPropertyPathExtension path ) {
103+ return path .isEntity () && !path .isEmbedded () ? path : this .getTableOwningAncestor (path .getParentPath ());
104+ }
105+
106+ private SqlIdentifier assembleTableAlias (PersistentPropertyPathExtension path ) {
107+
108+ Assert .state (path != null , "Path is null" );
109+
110+ PersistentPropertyPath <? extends RelationalPersistentProperty > propertyPath = path .getRequiredPersistentPropertyPath ();
111+ RelationalPersistentProperty leafProperty = propertyPath .getRequiredLeafProperty ();
112+
113+ String prefix ;
114+ if (path .isEmbedded ()) {
115+ prefix = leafProperty .getEmbeddedPrefix ();
116+ } else {
117+ SqlTableAlias sqlTableAlias = leafProperty .findPropertyOrOwnerAnnotation (SqlTableAlias .class );
118+ prefix = sqlTableAlias != null ? sqlTableAlias .value () : leafProperty .getName ();
119+ }
120+
121+ if (path .getLength () == 1 ) {
122+ Assert .notNull (prefix , "Prefix mus not be null." );
123+ return SqlIdentifier .quoted (prefix );
124+ }
125+
126+ PersistentPropertyPathExtension parentPath = path .getParentPath ();
127+ SqlIdentifier sqlIdentifier = this .assembleTableAlias (parentPath );
128+
129+ return parentPath .isEmbedded () ? sqlIdentifier .transform (name -> name .concat (prefix ))
130+ : sqlIdentifier .transform (name -> name + "_" + prefix );
131+ }
78132}
0 commit comments