Java Hibernate@ElementCollection-需要更好的解决方案
我正在使用Hibernate 3.5.a-Final作为web应用程序中的ORM层。我有几个bean使用相同的代码SnipletWich,这让我觉得这个设计不是最好的。但我不知道如何在hibernate中实现更好的 要求Java Hibernate@ElementCollection-需要更好的解决方案,java,hibernate,orm,jpa,Java,Hibernate,Orm,Jpa,我正在使用Hibernate 3.5.a-Final作为web应用程序中的ORM层。我有几个bean使用相同的代码SnipletWich,这让我觉得这个设计不是最好的。但我不知道如何在hibernate中实现更好的 要求 有几个类需要包含多个语言环境中的本地化描述 这些需要持久化到数据库中 它们必须可以通过子字符串搜索所有地区(如果seachstring是任何描述的子字符串,则显示) 本地化描述应该可以在不加载主对象的情况下进行查询(按主对象id、-类型和区域设置) 当前解决方案(不解决最后
- 有几个类需要包含多个语言环境中的本地化描述
- 这些需要持久化到数据库中
- 它们必须可以通过子字符串搜索所有地区(如果seachstring是任何描述的子字符串,则显示)
- 本地化描述应该可以在不加载主对象的情况下进行查询(按主对象id、-类型和区域设置)
@ElementCollection(fetch=FetchType.EAGER)
@CollectionTable(name = "localized[X]Descriptions", joinColumns = @JoinColumn(name = "id"))
@MapKeyJoinColumn(name = "locale")
public Map<Locale, String> getLocalizedDescriptions() {
return localizedDescriptions;
}
由于某种原因,@MapKeyJoinColumn
被忽略
我更喜欢这样一张桌子:
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)
)
@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;
}
}
如果可以使用CriteriaAPI查询实现(据我所知,它与@ElementCollection
s不兼容),这将是一个巨大的优势。
但我不知道如何实现这一点。任何建议都是非常受欢迎的我找到了自己的解决方案 我只是用
@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]
}
作为所有本地化描述的父类,并像
@Entity
public class LocalizedSomeDescription extends LocalizedDescription {
private Some master;
/**
* @return the master
*/
@ManyToOne
public Some getMaster() {
return master;
}
它的用法如下:
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)
)
@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;
}
}
在所有子类中使用mappedBy=“master”可能看起来像是滥用hibernate继承,但所有其他解决方案都会在每个子类中包含一行,而在每个子类中都是空的,这在我看来就像是一个非常糟糕的表设计。我仍然需要找出discriminatorType=discriminatorType.INTEGER的“合理默认值”是什么,以及是否需要覆盖该默认值