我试图拥有一个链接到多个表的本地化表。 我意识到问题在于我使用了本地化ID(例如Localization_Id),因此我无法在没有其他密钥的情况下链接到不同的本地化。我应该以某种方式在数据库中使用连接表或其他顺序ID吗?不确定使用JPA的最佳方法是什么。
提前致谢。
@Entity
public class MyEntityWithLocalization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
@JoinColumn(name="LOCALIZATION_KEY")
List<Localization> field1;
@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
@JoinColumn(name="LOCALIZATION_KEY")
List<Localization> field2; //can't be unique from field one as it links to the MyEntityWithLocalization id.
@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
@JoinColumn(name="LOCALIZATION_KEY")
List<Localization> field3; //can't be unique from field one as it links to the MyEntityWithLocalization id.
}
@Entity
public class Localization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
String language;
String string;
public Localization(String language, String string) {
this.language = language;
this.string = string;
}
public Localization(){
}
}
这会在本地化表中创建一个localization_key,但它只是键入MyEntityWithLocalization的ID - 它需要是另一个唯一值,这使我相信在这种情况下连接表可能有意义。
create table localization (
id number(19,0) not null,
language varchar2(255),
string varchar2(255),
localization_key number(19,0),
primary key (id)
);
答案 0 :(得分:0)
嗯。为什么不将实体拆分为常量和可本地化的部分?像这样:
@Entity
class MyEntity{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@OneToMany
@MapKeyColumn
Map<String, MyEntityLocalization> localizations;
}
@Embeddable
class MyEntityLocalization {
String field1;
String field2;
String field3;
}
本地化字段在哪里有从语言到本地化的映射?另一种方法是使用特定于Hibernate的注释:
@Entity
class MyEntityWithLocalization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@OneToMany
@MapKeyColumn(name="language")
@WhereJoinTable(clause = "key=1")
Map<String, String> field1;
@OneToMany
@MapKeyColumn(name="language")
@WhereJoinTable(clause = "key=2")
Map<String, String> field2;
}
@Entity
public class Localization {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
long key;
String language;
String string;
}