2009-06-04 13 views
10

Próbuję odwzorować obiekt @Embeddable w podklasie, której klasa nadrzędna ma już pole tego typu @Embeddable.Hibernacja mapowanie drugiego pola @Embeddable w podklasie

hibernate Embeddable Objects documentation zastrzeżeń można używać @AttributeOverrides przesłonić kolumn w obiekcie z @Embeddable:

np

@Entity 
public class Person implements Serializable { 

    // Persistent component using defaults 
    Address homeAddress; 

    @Embedded 
    @AttributeOverrides({ 
      @AttributeOverride(name="iso2", column = @Column(name="bornIso2")), 
      @AttributeOverride(name="name", column = @Column(name="bornCountryName")) 
    }) 
    Country bornIn; 
    ... 
} 

Oto przypadek mam:

@Embeddable 
    public class ContentID implements Serializable { 
     @Column(name="contentID") 
     private String contentPath; 
    } 

    @MappedSuperclass 
    public abstract class BaseDomainObject implements Serializable { 

     @Embedded 
     private ContentID contentID; 
    } 

public class Achievement extends BaseDomainObject { 

    @Embedded 
    @AttributeOverrides({ 
     @AttributeOverride(name="contentID", column = @Column(name="awardedItem")), 
    }) 
    private ContentID awardedItem; 
} 

Błąd pojawia się:

org.hibernate.MappingException: Powtarzające kolumna mapowania dla jednostki: kolumnie Achievement: contentID (powinien być zmapowany za pomocą insert = "false" update = "false") pod adresem org.hibernate.mapping.PersistentC lass.checkColumnDuplication (PersistentClass.java:652) w org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication (PersistentClass.java:674) w org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication (PersistentClass.java:670) w org.hibernate.mapping.PersistentClass.checkColumnDuplication (PersistentClass.java:696) w org.hibernate.mapping.PersistentClass.validate (PersistentClass.java:450) w org.hibernate.mapping.SingleTableSubclass.validate (SingleTableSubclass .java: 43) pod adresem org.hibernate.cfg.Configuration.validate (Configuration.java:1108) atorg.hibernate.cfg.Configuration.buildSessionFactory (Configuration.java:1293) w org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory (AnnotationConfiguration.java:867)

UPDATE:

Zaglądałem do problemów Hibernacji związanych z tym i projekt GRAILS twierdził, że naprawili ten problem, ale ich rozwiązanie do adnotacji nie wydaje się być poprawnymi adnotacjami javax.persistence (może jest to nowa wersja).

JPA @Embeddable/@Embedded throws org.hibernate.MappingException: Repeated column in mapping for entity

+0

Z ciekawości: Dlaczego osadzić kraj zamiast używania związek? –

Odpowiedz

7

Problem wydaje się być w tym:

public class ContentID implements Serializable { 
    @Column(name="contentID") 
    private String contentPath; 
} 

Robisz nazwę kolumny contentPath być „ContentID” i że jest zderzeniu z AttributeOverride adnotacji później.

Spróbuj tego:

public class ContentID implements Serializable { 
    @Column(name="contentPath") 
    private String contentPath; 
} 

UPDATE Ja też zastanawiałem się o tym:

@Embedded 
@AttributeOverrides({ 
    @AttributeOverride(name="contentID", column = @Column(name="awardedItem")), 
}) 
private ContentID awardedItem; 

Wydaje się, że zmiana nazwy kolumny ContentID żeby awardedItem. Czy to naprawdę konieczne?

+0

Problem nie jest spowodowany przez @AttributeOverride w osiągnięciu, ale z powodu @Embedded ContentID, który znajduje się w klasie bazowej Achievement BaseDomainObject. Problem polega na odwzorowaniu hibernacji, ponieważ dziedziczenie powoduje, że obiekt ma 2 obiekty osadzonego ContentID jako kolumnę contentID, którą uważałem za AttributeOverride. Nawet jeśli usunąłem @Column (name = "contentID"), myślę, że problem pozostałby. – Dougnukem

1

Używam

@JoinColumn(insertable=false, updatable=false) 

jako obejście.

2

Vincent ma rację. Pole Nazwa attributeOverride odnosi się do nazwy kolumny, która powinna być atrybutem/właściwością klasy.

@AttributeOverrides({ 
    @AttributeOverride(name="contentPath", column = @Column(name="awardedItem")), 
}) 

Należy zauważyć, że nazwa dotyczy właściwości klasy, a nie kolumny bazy danych.

Zobacz documentation

Powiązane problemy