Query Results Cache with OneToMany and "Fetch Join" [message #1729552] |
Fri, 15 April 2016 12:05 |
Rainer Schmand Messages: 4 Registered: March 2016 |
Junior Member |
|
|
the following scenario does not work like expected:
i am using eclipselink 2.6.2 and i have problem with my entity-mappings for translated content.
consider two tables "country" and "translation"
country:
COUNTRY_ID CODE TRANSLATION
1 DE 100
2 FR 200
translation:
TRANSLATION_ID LANGUAGE_ID CONTENT
100 1 Deutschland
100 2 Germany
100 3 Allemagne
200 1 Frankreich
200 2 France
200 3 France
Entity-Mapping "Country"
@NamedQueries({
@NamedQuery(
name = "Country.getAll",
query = "SELECT c FROM Country c JOIN FETCH c.translations t WHERE t.languageId = :language",
hints={
@QueryHint(name = QueryHints.QUERY_RESULTS_CACHE, value = HintValues.TRUE)
}
)
})
@Entity
public class Country {
@Id
private String id;
@Column
private String code;
@OneToMany
@JoinColumn(name = "translationId",referencedColumnName="translation" )
private List<Translation>translations;
public static List<Country> getAll(final int languageID) {
final Query userQuery = EntityManagerUtil.getEntityManager().createNamedQuery("Country.getAll");
userQuery.setParameter("language", languageID);
final List<Country>country= (List<Country>) userQuery.getResultList();
return country;
}
// getters and setters omitted
}
Entity-Mapping "Translation"
@Entity
public class Translation {
@Id
private Long translationId;
@Id
private int languageId;
@Column
private String content;
//getters and setters ommited
}
when calling "Country.getAll(1)" multiple times, everything is ok - the database is only hit once. but when the user changes the language and i call "Country.getAll(2);" translations never change; they always keep the previous ones although the right sql is logged.
when i take the sql with the logged parameters, the database gives me the desired results.
logged sql:
SELECT t1.COUNTRY_ID, t1.CODE, t0.TRANSLATION_ID, t0.LANGUAGE_ID, t0.CONTENT
FROM TRANSLATION t0, COUNTRY t1
WHERE ( (t0.LANGUAGE_ID = 2) ) AND (t0.TRANSLATION = t1.TRANSLATION_ID) )
how to get the updated values in my entity? any hint or tip here would be very appreciated. thanks in advance!
rainer
[Updated on: Fri, 15 April 2016 12:16] Report message to a moderator
|
|
|
|
Re: Query Results Cache with OneToMany and "Fetch Join" [message #1729670 is a reply to message #1729600] |
Sun, 17 April 2016 16:10 |
Rainer Schmand Messages: 4 Registered: March 2016 |
Junior Member |
|
|
Thank you for your clear answer. The only problem with your suggested query is, that the defintion for the result-cache will remain unaffected; The database is hit every time when using a query like "SELECT n1, n2, n3... FROM".
"Country.getAll",
query = "SELECT t, c FROM Country c JOIN c.translations t WHERE t.languageId = :language",
hints={ // this has no effect for above query
@QueryHint(name = QueryHints.QUERY_RESULTS_CACHE, value = HintValues.TRUE)
}
I can't figure out how to to enable the results-cache for such queries. Caching is required because the the countrycode from country-table and the translated countrynames have to be shown on many web-pages.
The results-cache only works when using
"SELECT c FROM Country WHERE x = :y"
[Updated on: Sun, 17 April 2016 17:49] Report message to a moderator
|
|
|
|
Re: Query Results Cache with OneToMany and "Fetch Join" [message #1729787 is a reply to message #1729770] |
Tue, 19 April 2016 06:09 |
Rainer Schmand Messages: 4 Registered: March 2016 |
Junior Member |
|
|
thank you chris. finally i am happy with the suggested ManyToOne Reference in the Translations-Entity:
@NamedQueries({
@NamedQuery(
name = "Translations.getAllCountriesByLanguage",
query = "SELECT t FROM Translations t JOIN FETCH t.country c WHERE t.languageId = :language",
hints={
@QueryHint(name = QueryHints.QUERY_RESULTS_CACHE, value = HintValues.TRUE),
}
)
})
@Entity
public class Translation {
@Id
private Long translationId;
@Id
private int languageId;
@Column
private String content;
@ManyToOne
@JoinColumn(name = "translationId",referencedColumnName="countryName", insertable=false, updatable=false )
private Country country;
}
@Entity
public class Country {
@Id
private String id;
@Column
private String code;
@Column
private Long countryName; //references Translations.translationId
}
[Updated on: Tue, 19 April 2016 06:11] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03669 seconds