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
@Entity and @Column seem not working while findNative by aggregate sql #2919
Comments
It would be good to know:
This feature uses jdbc metadata and so it can be impacted by the database and jdbc driver. If you can create a minimal test case that reproduces the issue perhaps based off https://github.com/ebean-orm-examples/example-minimal ? Side note: Although there isn't anything wrong with the approach here the same sort of thing can be done using DtoQuery. See https://ebean.io/docs/query/dtoquery . Take particular note of the mapping with respect to constructors. For example, in this example if the dto was a java record or class where the constructor took all 3 parameters then that would be used to map the result into the dto class. |
minimal test will be created later. |
And another question to be asked: If DotQuery is used, how can I specifiy field mappings over convention(No hint was found in doc)? Take this for example: I have a column named camel_style, I want to map it to realCamelStyle member variable? |
As I tried, the javax.persistence.PersistenceException: Query threw SQLException:Text '204' could not be parsed at index 0 Query was: select
effective_action_type,
CAST(min_action_time2 AS DATE) as effective_action_time,
count(*) as action_count
from
(
select
effective_action_type,
sign_id,
min(action_time2) as min_action_time2
from
(
select
sign_id,
action_time2,
CASE
WHEN final_action_type = 'wx_1166' THEN '405'
ELSE final_action_type
END AS effective_action_type
from
leads_web_action_data
where
action_time2 between '2022-12-13 00:00:00' and '2022-12-27 23:59:59'
order by
action_time2 desc
)
group by
effective_action_type,
sign_id
)
group by
effective_action_type,
CAST(min_action_time2 AS DATE) So, what's the best practice to use findDto with column mapping? |
Ok, apologies for the delay here. Edit:
Have you looked to create a failing test case yet? Note that your exception is not clear to me in that it does not include enough of the stack trace to understand what is throwing the exception. |
Did you create a minimal test case that reproduces the issue perhaps based off https://github.com/ebean-orm-examples/example-minimal ? |
DtoQuery mapping
Use a sql column alias ... and it can be either e.g using
e.g using
case does not matterThe way that database columns are mapped to logical java properties is by reading the column labels from the jdbc metaData. We can't guarantee the case (upper case/lower case) that the database dictionary returns the column labels but generally for most databases this is coming back in upper case. For this reason when mapping of database columns to bean properties ebean internally makes all the database columns/labels returned upper case for mapping purposes. Internally the mapping works with everything in upper case (database column labels and bean properties). camel vs snake _For Dto mapping from column label to bean property, given a label of "REAL_CAMEL_STYLE" the mapping will:
|
Nested SQLTo be explicit, when mapping ebean is using the jdbc resultSet metaData and so any nested sql has no impact. Only the top most select clause impacts the mapping as it defines what the jdbc resultSet metaData returns in terms of column names/labels. -- top most select clause, this controls the column names / labels jdbc metaData returns
select ....
from (
-- nested sql has NO impact on mapping
select ...
)
|
I'm thinking that you got busy with stuff. I'll close this for now and you can just re-open it when you get time to get back into this issue. Cheers, Rob. |
Sorry for late reply, thanks for you patience. Here is my basic example: LeadsWebActionDataDao My initial description was misleading because we were using Clickhouse as datasource instead of MySql, so I changed the comment above to show correct drivers. The example is not a complete runnable application, because I don't have a clickhouse for this project to connect, which needs a docker container to hold whole stack. But I think the domain class explains my issue. I made some comment above findNative() and findDto() to explain the issue, let me add something here: issue1: I supposed @Column(name = "action_count")
private Integer count; will help me to map a column result to a name which does not comply with camel conventional naming pattern, but I got empty record when using findNative api. As you can see my example above, I want to map "camel_style" in sql result to "realCamelStyle" in java, the extra "real" needs to be handled. I know I can modify the sql to use sql alias(so the sql column will be renamed as "real_camel_style"), but shouldn't the column mapping work as alias, too? issue 2: -- result will look like
-- 204, '2022-11-10 00:00:00', 10
select effective_action_type, effective_action_time, action_count ..... In java view, first column is String, the second column is Date, the third column is Integer. Then I create a class in arbitrary member order: public class LeadsWebActionDataCountAggregate2 {
@Column(name = "effective_action_time")
private Date effectiveActionTime;
@Column(name = "effective_action_type")
private String effectiveActionType;
@Column(name = "action_count")
private Integer actionCount;
} will throw an exception, when first column in result is a normal integer like 204, which should be mapped to effectiveActionType, but ebean tries to map it to effectiveActionTime (which is first member of class) when using findDto() api, then I got an casting exception: Integer can not be mapped to Date. Shouldn't @column deal with member sequence issue? |
I think if you as a collaborator closed this issue, I can't re-open it. Hope you can review this thread someday. |
The goal would be to git clone the repo, mvn clean test and see the issue. There isn't a test that runs that DAO. Are you able to change this into a test that runs and reproduces the issue? I realise the application is using clickhouse but we should be able to have a test use H2 that reproduces the issue? Noting the sql in the DAO looks invalid - it ends with |
Well, I don't know whether we can reproduce the issue with H2 jdbc connector(which I think causes different behavior), I will complete the test and get back to you later. |
Expected behavior
query some object by annotation-driven mapping, have result object with properties populated.
Actual behavior
The result is like:
Steps to reproduce
we are using ebean to mapped result from clickhouse:
the sql is like:
and the java bean is like:
the api invocation is like:
leadsWebActionDataCountAggregates: [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
The text was updated successfully, but these errors were encountered: