-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
Add duplicate column detection in EntityProvider #1267
Add duplicate column detection in EntityProvider #1267
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestion.
In general, duplicated columns are often considered a bug, so throwing an exception seems appropriate. However, considering that there might be users who rely on the current behavior, I think it would be better to make the DuplicateColumnException
optional.
Specifically, I propose the following changes:
- Introduce a new interface called
DuplicateColumnHandler
, with the current behavior set as the default implementation. - Add a new implementation class,
ThrowingDuplicateColumnHandler
, which throws aDuplicateColumnException
.
If you agree with this approach, could you update your pull request accordingly?
Alternatively, I can merge the current pull request as it is and handle the changes on my side. Please let me know your thoughts.
import org.seasar.doma.jdbc.ResultMappingException; | ||
import org.seasar.doma.jdbc.Sql; | ||
import org.seasar.doma.jdbc.UnknownColumnHandler; | ||
import org.seasar.doma.jdbc.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use wildcard (*
) imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import org.seasar.doma.jdbc.SqlLogType; | ||
import org.seasar.doma.jdbc.UnknownColumnException; | ||
import org.seasar.doma.jdbc.UnknownColumnHandler; | ||
import org.seasar.doma.jdbc.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use wildcard (*
) imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nakamura-to |
…DuplicateColumnException optional
…nException optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your fixes. I have added a few additional comments.
int count = resultSetMeta.getColumnCount(); | ||
for (int i = 1; i < count + 1; i++) { | ||
String columnName = resultSetMeta.getColumnLabel(i); | ||
String lowerCaseColumnName = columnName.toLowerCase(); | ||
if (!seenColumnNames.add(lowerCaseColumnName)) { | ||
duplicateColumnHandler.handle(query, columnName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you pass lowerCaseColumnName
instead of columnName
? The reason for this request is to align with the invocation of unknownColumnHandler.handle
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -111,6 +116,46 @@ public void testGetEntity_EmptyUnknownColumnHandler() throws Exception { | |||
assertEquals(100, emp.getVersion()); | |||
} | |||
|
|||
@Test | |||
public void testCreateIndexMap_DuplicateColumnNameException() throws SQLException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test case name includes "DuplicateColumnNameException," but the test does not throw a DuplicateColumnNameException
, which makes it confusing. Could you consider renaming it for clarity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for all your effort! |
PR Overview
This PR introduces improvements to Doma to handle scenarios where duplicate column names in query results could lead to unintended behavior during entity mapping. If this behavior is intentional in certain use cases, we are open to revisiting or withdrawing this change. The following enhancements have been implemented:
Duplicate Column Detection
DuplicateColumnException
, to provide detailed error information when duplicates are detected.Related Code Changes
EntityProvider
class with duplicate column detection logic.Message
enum.Background
When mapping query results to entities, properties are mapped based on column names or the
@Column
annotation'sname
attribute. However, in cases where multiple tables are joined, and columns with the same name exist without specific annotations like@Table
or@Column
, the column from the joined table would take precedence, leading to unexpected behavior.Investigation Findings
The following steps were taken to investigate the issue:
Two tables,
sample
andsample_sub
, were created, and a join query was executed as follows:sample_sub
SQL templates File
Entity
dao
When executed, the id column from the joined table (sample_sub) was mapped to the entity, as shown below: