我需要在JPA中覆盖collectiontable吗?也许有人有一个主意
public class nationality{
@Embedded
@AttributeOverrides({
@AttributeOverride(name="language.language", column = @Column(name="code_lang")) })
private Language language;
.....
语言课
@Embeddable
public class Language{
@ElementCollection(fetch = FetchType.Eager)
@CollectionTable(name="nat", joinColumns = @JoinColumn(name = "id"))
private Collection<LanguageObj> language;
...
}
这是可行的,但是@CollectionTable链接必须在国籍类中发生。如何才能做到这一点?
其他从其他表那里获取数据的类必须重用该语言类。
问题是表已经与数据一起存在,因此无法编辑该表
像是从表格国家/地区获取数据的课程国家/地区
public class country{
@Embedded
@AttributeOverrides({
@AttributeOverride(name="language.language", column = @Column(name="code_country")) })
private Language language;
.....
}
最佳答案
这是解决方案
总体思路是:
1.在可嵌入对象中,使用@ElementCollection
注释收集字段
2.在实体中,使用@CollectionTable
注释集合getter。吸气剂应前进到可嵌入的吸气剂。对于每个实体,您可以在@CollectionTable
批注内指定单独的表名
enum DayOfWeek {
MONDAY,TUESDAY,...
}
@Embeddable
class ScheduleProperties {
...
@ElementCollection
private List<DayOfWeek> daysOfWeek;
...
public List<DayOfWeek> getDaysOfWeek() {
return daysOfWeek;
}
}
@Entity
class SomeEntity {
...
@Embedded
private ScheduleProperties embd = new ScheduleProperties();
...
@CollectionTable(name="some_entity_days_of_week")
public List<DayOfWeek> getDaysOfWeek() {
return embd.getDaysOfWeek();
}
}
@Entity
class AnotherEntity {
...
@Embedded
private ScheduleProperties embd = new ScheduleProperties();
...
@CollectionTable(name="another_entity_days_of_week")
public List<DayOfWeek> getDaysOfWeek() {
return embd.getDaysOfWeek();
}
}