Hibernate @ElementCollection - 需要更好的解决方案 [英] Hibernate @ElementCollection - Better solution needed

查看:150
本文介绍了Hibernate @ElementCollection - 需要更好的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web应用程序中使用Hibernate 3.5.a-Final作为ORM-Layer。我有几个Beans有相同的代码片段,这让我觉得这个设计并不是最好的。但我无法弄清楚如何在hibernate中实现更好的。

I'm using Hibernate 3.5.a-Final as ORM-Layer in a web application. I've got several Beans with the same code-sniplet wich makes me think that this design isn't the best one around. But I can't figure out how to implement a better one in hibernate.

要求


  • 多个类需要在多个语言环境中包含本地化描述

  • 这些需要保存到数据库中

  • 它们必须可以通过以下方式搜索:所有语言环境的子字符串(如果seachstring是任何描述的子字符串,则显示)

  • 本地化描述应该是可查询的,无需加载主对象(通过master-object-id,-type和区域设置)

  • Several classes need to contain localized descriptions in multiple locales
  • These need to be persisted into the db
  • They have to be searchable by substring for all locales (show up if the seachstring is a substring of any description)
  • Localized descriptions should be queryable without loading the master-object (by master-object-id, -type and locale)

当前解决方案(不解决上一个要求)

Current solution (doesn't solve the last requirement)

每个类包含一个注释为的HashMap

Each class contains a HashMap annotated as

@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name = "localized[X]Descriptions", joinColumns = @JoinColumn(name = "id"))
@MapKeyJoinColumn(name = "locale")
public Map<Locale, String> getLocalizedDescriptions() {
    return localizedDescriptions;
}

[X]该类的名称

对于每个类,这是一个附加表(由hibernate生成)

For Each class the is an additional table (generated by hibernate)

create table localized[X]Descriptions (
    id integer not null,
    localizedDescriptions varchar(255),
    localizedDescriptions_KEY varchar(255),
    primary key (id, localizedDescriptions_KEY)
)

由于某种原因,忽略 @MapKeyJoinColumn 。 。

For some reason the @MapKeyJoinColumn gets ignored...

我更喜欢的是这样一张桌子:

What I'd prefer would be a single table like this:

create table localizedDescriptions (
    class varchar(255) not null,
    id integer not null,
    locale varchar(50) not null,
    description varchar(255) not null,
    primary key (class, id, locale)
)

It如果使用criteria-api(与不兼容)可以查询实现,那将是一个很大的优势@ElementCollection s据我所知)。
但我无法弄清楚如何实现这一点。任何指针都会非常受欢迎

It would be a big plus if the implementation would be queryable using the criteria-api (which isn't compatible to @ElementCollections as far as I know). But I can't figure out how to implement this. Any pointers would be very welcome

推荐答案

我找到了自己的解决方案......

I found my own solution...

我只是使用

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="masterClass", discriminatorType=DiscriminatorType.INTEGER)
@Table(name="localizedDescriptions")
public class LocalizedDescriptions{

    private Integer id;
    private Locale locale;
    private String description;

        [Getters, Setters]
}

作为我的父母-class用于所有本地化描述并扩展它,如

as my parent-class for all localized descriptions and extend it like

@Entity
public class LocalizedSomeDescription extends LocalizedDescription {

    private Some master;

    /**
     * @return the master
     */
    @ManyToOne
    public Some getMaster() {
        return master;
    }

这样使用:

@Table
@Entity
public class Some {

    private Map<Locale, LocalizedSomeDescription> names = new HashMap<Locale, LocalizedSomeDescription>();

    @OneToMany
    @JoinColumn(name="master_id")
    @MapKeyColumn(name="locale")
    public Map<Locale, LocalizedSomeDescription> getDescriptions() {
        return descriptions;
    }

}

这导致非常类似于我想要的表格设计

This results in something very similar to my intended table design

create table localizedDescriptionss (
    masterClass integer not null,
    id integer not null auto_increment,
    locale varchar(255),
    description varchar(255),
    master_id integer,
    primary key (id)
)

在所有子类中使用mappedBy =master可能看起来像滥用hibernate继承,但所有其他解决方案将包括每个子类一行在其他每一个都是null,这在我看来就像一个非常糟糕的表设计。我仍然需要找出 discriminatorType = DiscriminatorType.INTEGER 的合理默认值,如果我需要覆盖该默认值。

using mappedBy="master" in all subclasses might seem like an abuse of hibernate inheritance but all other solutions would include one row per subclass which would be null in every other, which seems to me like a very bad table design. I still have to find out what's the 'sensible default' for a discriminatorType=DiscriminatorType.INTEGER and if I need to override that default.

这篇关于Hibernate @ElementCollection - 需要更好的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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