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

add prefix in generated eager relationship queries #14658

Merged
merged 6 commits into from
Apr 14, 2021
Merged

add prefix in generated eager relationship queries #14658

merged 6 commits into from
Apr 14, 2021

Conversation

jidma
Copy link
Contributor

@jidma jidma commented Apr 12, 2021

Closes #14626

When using a table name with an SQL keyword (example: Order), the generated query throws an exception:

Example:

    @Query("select distinct order from Order order left join fetch order.products")
    List<Order> findAllWithEagerRelationships();

Produces the following error:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: order near line 1, column 17 [select distinct order from com.mycompany.myapp.domain.Order order left join fetch order.products]
        at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
        at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:301)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162)
        at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:613)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:725)
        ... 71 common frames omitted

This PR adds the prefix to the instance name in such case (order -> jhiOrder):

    @Query("select distinct jhiOrder from Order jhiOrder left join fetch jhiOrder.products")
    List<Order> findAllWithEagerRelationships();
Tested with the following jdl
entity Product {
    name String
}

entity Client {
    name String
}

entity Order {
    name String
}

entity Left {
    name String
}

relationship ManyToMany {
    Order{product(name)} to  Product{order} 
    Product{desc(name)} to  Left{product} 
    Client{product(name)} to  Product{product} 
    Left{client(name)} to Client{desc(name)} 
}

relationship ManyToOne {
    Order{user(login)} to  User
    Left{user(login)} to  User
}

Please make sure the below checklist is followed for Pull Requests.

When you are still working on the PR, consider converting it to Draft (bellow reviewers) and adding skip-ci label, you can still see CI build result at your branch.

@jidma jidma changed the title add suffix in generated eager relationship queries add prefix in generated eager relationship queries Apr 12, 2021
@jidma jidma marked this pull request as ready for review April 12, 2021 19:12
utils/relationship.js Outdated Show resolved Hide resolved
@jidma jidma marked this pull request as draft April 12, 2021 22:13
Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
@jidma jidma marked this pull request as ready for review April 12, 2021 22:54
@jidma jidma requested a review from mshima April 12, 2021 22:59
Copy link
Member

@mshima mshima left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java.lang.IllegalArgumentException when relationship ManyToMany
4 participants