I am building a category-based web application multilanguage using JPA/Hibernate + Spring
I currently built these three beans/entities: Category, CategoryLanguage and Language.
Category:
@Entity
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
private Category father;
@OneToMany(mappedBy = "father")
private List<Category> children;
@OneToMany(mappedBy="category")
private List<CategoryLanguage> categoryLanguages;
CategoryLanguage:
@Entity
public class CategoryLanguage implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
Category category;
@Column(length = 20, nullable = false)
private String name;
@ManyToOne
private Language language;
and Language
public class Language implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(length=2)
private String id; // en, de, fr, it, es,...
@Column(length=25)
private String name; // English, Deutsch, Français,...
By calling
from Category c join c.categoryLanguages cl where c.father = null and cl.language.id = :lang
It returns the first level of categories with the right language, but contains all the languages as children.
Children are extracted for every language instead of the one I chose on the query.
What could I do to solve this problem? I can not use "join on" in JPQL, can i?
Your query is asking for complete Category entities, and so you cannot accomplish what you want strictly with JPA. JPA requires that the category entities returned reflect the data that is in the database - this includes all CategoryLanguages regardless of the language they might correspond to in the selecting query.
What you might want to do instead is to rearrange the query differently. In this case, query for the CategoryLanguage entities associated to the Language you want, and then use it to obtain the Category. The category->CategoryLanguage relation will still be populated, but your query only returns the CategoryLanguage representing the language you want without any other languages. Key these by Category to make it easier if you want.
Most providers do allow adding filtering criteria to the mappings directly, or you can even use queries that only return partial entities, but I caution you to avoid going down that route. Once you do, it is difficult to maintain the app and manage the data correctly, and can end up limiting performance options such as caching. For instance, once you have all the partial category entity and want to add a new CategoryLanguage or make any other change, how do you handle it? Merge would not work since it would cause any CategoryLanguage not in the list from the database
Make sure to use FetchType.EAGER
Use as below :
SELECT c FROM Cagegory c INNER JOIN c.categoryLanguages l WHERE c.father = null AND l.id = :languageId
@OneToMany(mappedBy = "father", fetch = FetchType.EAGER)
private List<Category> children = new ArrayList();
@OneToMany(mappedBy="category", fetch = FetchType.EAGER)
private List<CategoryLanguage> categoryLanguages = new ArrayList();