Skip to content

feat: Add attachJoinRef extension method#86

Merged
chinhtrung merged 4 commits intomainfrom
trungnguyen.implementattachJoinRef
Jan 14, 2026
Merged

feat: Add attachJoinRef extension method#86
chinhtrung merged 4 commits intomainfrom
trungnguyen.implementattachJoinRef

Conversation

@chinhtrung
Copy link
Contributor

Summary

Add attachJoinRef extension method to TypeSafeCriteriaBuilder that creates joins and returns both the criteria and join reference for reuse.

Motivation

I have seen some cases that we implement a base helper method that can be reused in different query methods. However, it's a bit troublesome when the usage wants to reuse the join reference that has been called in the base query. This method help us ease the process.

What's Changed

  • Added getOrCreateJoinRef method that prevents duplicate join errors by reusing existing joins
  • Added attachJoinRef extension method that returns a TypeSafeCriteriaWithJoinRef wrapper
  • Created TypeSafeCriteriaWithJoinRef data class to hold both criteria and join reference
  • Added comprehensive tests demonstrating basic usage and duplicate join prevention

Benefits

  • Enables fluent API for creating reusable join references
  • Prevents duplicate join errors when the same join is used multiple times
  • Improves query building ergonomics by providing both criteria and join ref in one call

Example Usage

val result = session.query(BookTable).attachJoinRef { author }
result.criteria.applyJoinRef(result.joinRef) { authors ->
    addLike(authors.name, "J.%")
}


// Check if a join already exists for this column definition
val existingJoin = query.joins.find { existingJoin ->
existingJoin.columnDef === joinColumnDef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will column def equality work like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I think so, I think we want to check the same object reference in this case.

Copy link
Member

@luanpotter luanpotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern with getOrCreateJoinRef is that when it is hidden within attachJoinRef it makes it a bit less clear and more magical that you might be re-using joins. the design behind having join refs in the first place was to separate intent on re-using a join versus creating a duplicate join (which is totally valid in SQL). having such a function will hide this distinction, making the existence of join refs in the first place less grounded

@chinhtrung chinhtrung requested a review from luanpotter January 13, 2026 23:38
Copy link
Member

@luanpotter luanpotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chinhtrung chinhtrung enabled auto-merge (squash) January 14, 2026 03:42
@chinhtrung chinhtrung merged commit 8d22199 into main Jan 14, 2026
7 checks passed
@chinhtrung chinhtrung deleted the trungnguyen.implementattachJoinRef branch January 14, 2026 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants