15

I'm trying to build a JPA specification for a I18N implementation to be able to filter on a name.
In the database I have translations for multiple languages.
The thing is that most of the times only the default language is specified.
So when someone requests the translation for a non default language I want it to fall back on the default language.

Until now I was able to build this in Specifications

Specification<S> specification = (root, query, cb) -> {
  Join i18n = root.join("translations", JoinType.LEFT);
  i18n.alias("i18n");
  Join i18nDefault = root.join("translations", JoinType.LEFT);
  i18nDefault.alias("i18nDefault");

  i18n.on(cb.and(cb.equal(i18n.get("itemId"), root.get("itemId")), cb.equal(i18n.get("languageId"), 1)));
  i18nDefault.on(cb.and(cb.equal(i18nDefault.get("itemId"), root.get("itemId")), cb.equal(i18nDefault.get("languageId"), 22)));

  // Clauses and return stuff
};

But this causes an error which sounds like bad news for this solution

org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause referenced two different from-clause elements
[
 select generatedAlias0 from com.something.Item as generatedAlias0 
 left join generatedAlias0.i18n as i18n with (i18n.itemId=generatedAlias0.itemId) and ( i18n.languageId=1L )
 left join generatedAlias0.i18n as i18nDefault with (i18nDefault.itemId=generatedAlias0.itemId) and ( i18nDefault.languageId=1L )
];

So Hibernate doesn't allow me to build a with-clause with different elements (in this case itemId and languageId).
Is there any way how I can implement this correctly or in a different way?

In the end I'd like Hibernate to generate a query (Oracle) that looks like this

SELECT *
FROM item i
LEFT JOIN i18n_item i18n ON i.item_id = i18n.item_id AND i18n.language_id = 22
LEFT JOIN i18n_item i18n_default ON i.item_id = i18n_default.item_id AND i18n_default.language_id = 1
// where-clauses;


Solving this using a where clause


I've seen a few other related questions that all have answers that say to use a where clause instead of a second join criteria. The thing is that the translations for the language that is not default may be missing for some records of the item. As the users are the ones that specify the translations for their content (which they also manage).

For example, The user might have specified 200 translation records for the items that are meant for English, but only 190 translation records for the items that are meant for Dutch. English is the default language in the example above.

I tried to build a query using a where clause, but that caused the amount of results to be inconsistent. By which I mean that unfiltered result would be 200 items. When I filter on name using like '%a%' it would return 150 results and when I flip the filter (not like '%a%') it would return something like 30. Where I would expect them to add up to 200.

This works

SELECT *
FROM item i
LEFT JOIN i18n_item i18n ON i.item_id = i18n.item_id AND i18n.language_id = 22
LEFT JOIN i18n_item i18n_default ON i.item_id = i18n_default.item_id AND i18n_default.language_id = 1
WHERE 
(
  (lower(i18n.name) not like '%a%') 
or 
  (i18n.name is null and (lower(i18n_default.name) not like '%a%'))
)

This does not work (doesn't return the correct amount of elements)

SELECT *
FROM item i
LEFT JOIN i18n_item i18n ON i.item_id = i18n.item_id
LEFT JOIN i18n_item i18n_default ON i.item_id = i18n_default.item_id
WHERE 
(
  (i18n.language_id = 22 and lower(i18n.name) not like '%a%') 
or 
  (i18n_default.language_id = 1 and i18n.name is null and (lower(i18n_default.name) not like '%a%'))
)

Is there a way to implemented the query, that works, using JPA specifications?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Robin Hermans
  • 1,579
  • 1
  • 24
  • 52
  • I noticed that this ticket adds support for explicit joins, which is what I need: https://hibernate.atlassian.net/browse/HHH-16. As far as I can see it's supported from Hibernate 5.1.0 – Robin Hermans Apr 07 '17 at 19:06

1 Answers1

12

After a weekend of research I found out that Hibernate 5.1.0 packs a new feature that allows you to build a join using criteria of multiple columns/fields. It's called cross-join (see comments).

See this ticket and this commit.

I asked around on the Spring Data Gitter about when this will be available for use with Spring Boot 1.x. They pointed me to setting the hibernate.version property in my maven or gradle file. Hence this only seems to work when you use the org.springframework.boot gradle plugin.

apply plugin: "org.springframework.boot"

Because I use Gradle I ended up adding the following to my build.gradle

ext {
    set('hibernate.version', '5.1.0.Final')
}

Or when using a gradle.properties file you can just put this line in there

hibernate.version=5.1.0.Final

And finally I'm able to do this

Specification<S> specification = (root, query, cb) -> {
  Join i18nJoin = root.join(collectionName, JoinType.LEFT);
  Join i18nDefaultJoin = root.join(collectionName, JoinType.LEFT);

  i18nJoin.on(cb.equal(i18nJoin.get("languageId"), 22));
  i18nDefaultJoin.on(cb.equal(i18nDefaultJoin.get("languageId"), 1));

  ... where clause and return ...
}

Which results in the following query

SELECT *
FROM item i
LEFT JOIN i18n_item i18n ON i.item_id = i18n.item_id and i18n.language_id = 22
LEFT JOIN i18n_item i18n_default ON i.item_id = i18n_default.item_id i18n_default.language_id = 1

Notice that using the on method doesn't overwrite the original clause set by the association annotation (in this case @OneToMany), but extends it with your own criteria.

Robin Hermans
  • 1,579
  • 1
  • 24
  • 52
  • 1
    wanted to post the same thing :) (I have only noticed the question now) this is called `cross-join` (be able to join other fields then the ones present in the mapping itself). And, indeed, as you have discovered it is present in hibernate 5 and above. – Eugene Apr 10 '17 at 12:33
  • Thanks for the name (`cross-join`) :). Added it to the answer. It's been bugging me for a whole week. I needed to get this working in order to complete the I18n implementation. In particular searching on the i18n fields. I'm glad this feature is now part of Hibernate (I guess more people are after reading the comments in [the ticket](https://hibernate.atlassian.net/browse/hhh-16)) – Robin Hermans Apr 10 '17 at 12:40
  • sorry to hear... I could have helped faster if I only have seen this faster :) – Eugene Apr 10 '17 at 12:43