Anular @CollectionTable

I need to override a collectiontable in JPA? maybe someone has an idea

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

The language class

@Embeddable
public class Language{
  @ElementCollection(fetch = FetchType.Eager)
  @CollectionTable(name="nat", joinColumns = @JoinColumn(name = "id"))
  private Collection<LanguageObj> language;
...
}

This is working, but the @CollectionTable linking must occure in the nationality class. How can this be done?

The class Language must be reused by other classes who get there data from a different table. The problem is that the tables already exists with data, so the table can not be edited like the class country who get his data from the table country

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

preguntado el 02 de febrero de 12 a las 10:02

¿Qué estás tratando de lograr exactamente? -

2 Respuestas

Esta es la solucion
La idea general es:
1. In the embeddable object annotate collection field with @ElementCollection
2. In the entities annotate collection getter with @CollectionTable. The getter should forward to the getter of embeddable. For each entity you can specify separate table name inside @CollectionTable anotación

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();
    }
}

Respondido 08 Oct 12, 23:10

Spent half day trying to override table for @ElementCollection. Finally got the successful result. Have no real explanation why it works. But it is! The general idea is: 1. In embeddable object annotate field with @ElementsCollection 2. In the entities annotate getter with @CollectionTable. The getter should forward to the getter of embeddable - Slava Medvediev

could you take a look at my question/answer. any hints why it isn't working. are some additional hibernate-configuration-changes needed? - cproinger

Its quite some time now, but this solution seems not to work. Tried it with hibernate as provider. - Dudelilama

Utilizan @AssociationOverride(s) como usarías @AttributeOverride(s)

@AssociationOverride(name = "language",
                     joinTable = @JoinTable(name = "nat",
                                            joinColumns = @JoinColumn(name = "id")))

Respondido 15 Oct 15, 17:10

not working, still uses the old @CollectionTable(schema=) valor - Hendy Irawan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.