gpt4 book ai didi

java - 覆盖@CollectionTable

转载 作者:行者123 更新时间:2023-12-04 18:34:44 27 4
gpt4 key购买 nike

我需要覆盖 JPA 中的集合表吗?也许有人有想法

 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 链接必须出现在国籍类中。如何做到这一点?

Language 类必须被从不同表中获取数据的其他类重用。问题是表已经存在数据,所以无法编辑表比如从表country中获取数据的class country

public class country{
@Embedded
@AttributeOverrides({
@AttributeOverride(name="language.language", column = @Column(name="code_country")) })
private Language language;
.....
}

最佳答案

解决办法
总体思路是:
1.在可嵌入对象中用@ElementCollection
注解集合字段 2. 在实体中使用@CollectionTable 注释collection getter。 getter 应该转发给 embeddable 的 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();
}
}

关于java - 覆盖@CollectionTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9110770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com