JPA类别 - 语言关系 [英] JPA category-language relationship

查看:103
本文介绍了JPA类别 - 语言关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA / Hibernate + Spring构建一个基于类别的Web应用程序多语言



我目前构建了这三个bean /实体:Category,CategoryLanguage和Language。 / p>

类别:
$ b

  @Entity 
public class Category实现Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne
私人类别父亲;
@OneToMany(mappedBy =父亲)
私人列表<类别>儿童;
@OneToMany(mappedBy =category)
private List< CategoryLanguage> categoryLanguages;

CategoryLanguage:


@GeneratedValue(strategy = GenerationType.AUTO)
private int id ;
@ManyToOne
分类类别;
@Column(length = 20,nullable = false)
私有字符串名称;
@ManyToOne
私有语言语言;

语言

  public class Language实现Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(长度= 2)
私人字符串ID; // en,de,fr,it,es,...

@Column(length = 25)
私有字符串名称; // English,Deutsch,Français,...

通过调用

 来自类别c join c.categoryLanguages cl其中c.father = null和cl.language.id =:lang 

它用正确的语言返回第一级别的类别,但包含所有语言为儿童。



为每种语言提取孩子,而不是我在查询中选择的孩子。

我能做些什么来解决这个问题?我不能在JPQL中使用join ,我可以吗?解析方案

对于完整的类别实体,因此您无法完全按照您的要求严格使用JPA。 JPA要求返回的类别实体反映数据库中的数据 - 这包括所有的CategoryLanguage,而不管它们在选择查询中可能对应的语言。

您可能想要做的事情是以不同的方式重新排列查询。在这种情况下,请查询与您想要的语言相关联的CategoryLanguage实体,然后使用它来获取类别。 category-> CategoryLanguage关系仍然会被填充,但是您的查询仅返回表示您希望使用的语言的CategoryLanguage而不使用任何其他语言。大多数提供程序都允许直接向映射添加过滤条件,或者甚至可以使用仅返回部分实体的查询,但我警告你避免走这条路。一旦这样做,就很难维护应用程序并正确管理数据,并最终会限制性能选项,如缓存。例如,一旦你拥有了所有的部分类别实体并且想要添加一个新的类别语言或进行其他更改,那么你如何处理它?合并不起作用,因为它会导致数据库中的任何CategoryLanguage不在列表中


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

这篇关于JPA类别 - 语言关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆