Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve how identifiers are treated #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stIncMale
Copy link
Member

No description provided.

@stIncMale stIncMale self-assigned this Apr 4, 2025
@stIncMale stIncMale force-pushed the improveHowItentifiersAreTreated branch from 2ef6d47 to 4f9ef0e Compare April 4, 2025 22:33

@Override
public @Nullable String toQuotedIdentifier(@Nullable String name) {
return name;
Copy link
Member Author

Choose a reason for hiding this comment

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

This single change makes all the new tests pass, and without it, they all fail. Nothing else related to quoting both in Dialect (including Dialect.buildIdentifierHelper) and in DatabaseMetaData had any observable effect on the tests added.

Copy link
Contributor

@NathanQingyangXu NathanQingyangXu Apr 6, 2025

Choose a reason for hiding this comment

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

It is a great news! Thanks for your PR. With some minor changes it could be merged.

static final String COLLECTION_NAME = "\"collection in double quotes Mixed Case\"";
static final String FIELD_NAME = "\"field in double quotes Mixed Case\"";
static final String ACTUAL_COLLECTION_NAME = "collection in double quotes Mixed Case";
static final String ACTUAL_FIELD_NAME = "field in double quotes Mixed Case";
Copy link
Member Author

@stIncMale stIncMale Apr 4, 2025

Choose a reason for hiding this comment

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

Unfortunately, it seems impossible to prevent Hibernate ORM from stripping identifiers like \"collection in double quotes Mixed Case\" of their quotes. Apparently, it has logic somewhere that ignores any configuration/overrides related to quoting, and strips double and single quotes anyway.

Copy link
Member Author

@stIncMale stIncMale Apr 4, 2025

Choose a reason for hiding this comment

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

That place seems to be here https://github.com/hibernate/hibernate-orm/blob/0a4d551b32801ae15e99d2f8ac0750303ecda2b6/hibernate-core/src/main/java/org/hibernate/boot/model/naming/Identifier.java#L159-L173 - it always considers identifiers enclosed in a set of statically-defined characters as quoted.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, it seems so. If the Hibernate quoting at the start and end is hard-coded or not configurable, I think we could remove those MixedCases testing cases for we don't need to test Hibernate if its behaviour is working as expected.

@Table(name = WithSpaceAndDotMixedCase.COLLECTION_NAME)
static class WithSpaceAndDotMixedCase {
static final String COLLECTION_NAME = "collection with space and .dot Mixed Case";
static final String FIELD_NAME = "field with space Mixed Case";
Copy link
Contributor

Choose a reason for hiding this comment

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

so the dot was intentionally excluded in field name, but should we still use Mixed Case if only space is there?
It would be great to leave some comment here to explain; otherwise for newcomer it seems some overlooking instead.

I think Mixed Case is only meant for mixture of both lower and upper cases. Seems Mixed suffices to express the intentions in this class.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems nontrivial to adhere to defensitive programming to provide relevant exception when field name contains .. Currently insertion is allowed but exception is thrown as follows from MongoResultSet:

java.sql.SQLException: Failed to get value from column [index: x]

for this PR one-liner comment to explain the rationale of missing dot seems enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

it seems more sensible to separate collection name identifier from field identifier classes, though I am ok with current status.


@Entity
@Table(name = InSingleQuotesMixedCase.COLLECTION_NAME)
static class InSingleQuotesMixedCase {
Copy link
Contributor

Choose a reason for hiding this comment

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

the MixedCase is a little bit unclear, though further code reading shows that the Mixed case simply means single quotes both at the beginning and at the end.

Optional. Avoid the confusing MixedCase here by using more intuitive verbiage like InSingleQuotesDelimiters or simply StartingAndEndingWithSingleQuote.

static final String COLLECTION_NAME = "`collection in single quotes Mixed Case`";
static final String FIELD_NAME = "`field in single quotes Mixed Case`";
static final String ACTUAL_COLLECTION_NAME = "collection in single quotes Mixed Case";
static final String ACTUAL_FIELD_NAME = "field in single quotes Mixed Case";
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, Hibernate has its own quotes so the above ACTUAL IDENTIFIER makes sense, as long as the quote pairs surround the whole identifier. For completeness, we could cover [] as well.

}

@Entity
@Table(name = InSingleQuotesMixedCase.COLLECTION_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

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

technically speaking, ``` is not the same as '. So more accurate verbiage is `backtick`, not `single quote`.

static final String COLLECTION_NAME = "\"collection in double quotes Mixed Case\"";
static final String FIELD_NAME = "\"field in double quotes Mixed Case\"";
static final String ACTUAL_COLLECTION_NAME = "collection in double quotes Mixed Case";
static final String ACTUAL_FIELD_NAME = "field in double quotes Mixed Case";
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, it seems so. If the Hibernate quoting at the start and end is hard-coded or not configurable, I think we could remove those MixedCases testing cases for we don't need to test Hibernate if its behaviour is working as expected.

@Column(name = EndingWithDoubleQuote.FIELD_NAME)
int v;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

seems EndingWithDoubleQuoteOnly might be more accurate for the MixedCase ends in the same way as well, right?


@Override
public @Nullable String toQuotedIdentifier(@Nullable String name) {
return name;
Copy link
Contributor

@NathanQingyangXu NathanQingyangXu Apr 6, 2025

Choose a reason for hiding this comment

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

It is a great news! Thanks for your PR. With some minor changes it could be merged.

.containsExactly(new BsonDocument()
.append(ID_FIELD_NAME, new BsonInt32(item.id))
.append(EndingWithDoubleQuote.FIELD_NAME, new BsonInt32(item.v)));
sessionFactoryScope.inTransaction(session -> session.get(EndingWithDoubleQuote.class, item.id));
Copy link
Contributor

Choose a reason for hiding this comment

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

the above testing cases seem to follow similar pattern and is paramerized by two inputs: 1) entity class; 2) identifier name supplier, so it is doable to avoid the duplication by JUnit 5's feature.

but this is optional and over-abstracting in testing code not necessarily helps on code maintaining.

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