覆盖@CollectionTable

时间:2012-02-02 10:31:32

标签: java hibernate jpa

我需要覆盖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链接必须在国籍类中出现。怎么办呢?

类语言必须由从其他表中获取数据的其他类重用。 问题是表已存在数据,因此无法编辑表 比如从国家/地区获取数据的国家/地区

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

2 个答案:

答案 0 :(得分:1)

这是解决方案
一般的想法是:
 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();
    }
}

答案 1 :(得分:-1)

像使用@AssociationOverride(s)

一样使用@AttributeOverride(s)
@AssociationOverride(name = "language",
                     joinTable = @JoinTable(name = "nat",
                                            joinColumns = @JoinColumn(name = "id")))