簡體   English   中英

多對多JPA Hibernate通過連接表錯誤映射組合鍵

[英]Many-to-Many JPA Hibernate mapping composite key through join table error

我有以下兩個類(及其pk類)和這些注釋。 我刪除了setter / getters / hashcode / equals以壓縮示例

我最終得到了這個錯誤

org.hibernate.MappingException: Repeated column in mapping for collection: com.stackOverflow.Features.thingCategory column: YEAR

我的猜測是因為“年”在連接表中共享,我搞砸了一些用於關聯我的實體的語法。 請注意,這些視圖在其實體之間具有隱式關系,我試圖在我的注釋中建模。 我嘗試了一些JPA建模工具,它們在建模后給出了相同的錯誤。 我已經嘗試將連接列設置為可insertableupdatablefalse

我當然可以寫一個SQL查詢,但我真的很驚訝Hibernate在我看來有多難。

特征實體

@Entity
@Table(name = "myTableOfFeatures")
public class Feature {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private FeatureKey id;

    @Column(name="displayText")
    private String description;

    //bi-directional many-to-many association
    @ManyToMany
    @JoinTable(
            name="joinTableToThingCategories"
            , joinColumns={
            @JoinColumn(name="name", referencedColumnName="name", insertable = false, updatable = false),
            @JoinColumn(name="year", referencedColumnName="year", insertable = false, updatable = false)
    }
            , inverseJoinColumns={
            @JoinColumn(name="year", referencedColumnName="year", insertable = false, updatable = false),
            @JoinColumn(name="title", referencedColumnName="title", insertable = false, updatable = false)
    }
    )
    private List<ThingCategory> thingCategory;

    public Feature() {
    } 
    // ... gets and sets

} 
@Embeddable
public class FeatureKey implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="name")
    private String myName;

    private String year;

    public FeatureKey() {
    }
    // ... gets and sets and equals and hashes
}

ThingCategory實體

@Entity
@Table(name = "CategoriesOfThings")
public class ThingCategory implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ThingCategoryKey id;

    private String comment;

    //bi-directional many-to-many association to categories
    @ManyToMany(mappedBy="thingCategory")
    private List<Feature> features;

    public ThingCategory() {
    }
    // ... gets and sets
}
@Embeddable
public class ThingCategoryKey implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    private String year;

    @Column(name="categoryName")
    private String title;

    public ThingCategoryKey() {
    }
    // ... gets and sets and equals and hashes
}

這是JPA 2.1規范的一個例子。 我希望它有所幫助。

@Entity
public class Employee {
    @Id int id;
    @Embedded ContactInfo contactInfo;
    ...
}


@Embeddable
public class ContactInfo {
    @ManyToOne Address address; // Unidirectional
    @ManyToMany List<PhoneNumber> phoneNumbers; // Bidirectional 
}

@Entity
public class PhoneNumber {
    @Id int phNumber; 

    @ManyToMany(mappedBy="contactInfo.phoneNumbers")         
    Collection<Employee> employees;
}

我強烈推薦你這個規范。 JPA 2.1

這可能有用,但我沒有嘗試過; 所以我想把它寫成評論,但把代碼寫成評論很麻煩。 所以你可能想嘗試它,如果它工作(抱歉沒有時間嘗試)。 如果它不起作用,請給我留言,我會刪除它。

@JoinTable(
        name="joinTableToThingCategories", 
        joinColumns={
            @JoinColumn(name="feature_name", referencedColumnName="name", insertable = false, updatable = false),
            @JoinColumn(name="feature_year", referencedColumnName="year", insertable = false, updatable = false)
        }, 
        inverseJoinColumns={
            @JoinColumn(name="tcat_year", referencedColumnName="year", insertable = false, updatable = false),
            @JoinColumn(name="tcat_title", referencedColumnName="title", insertable = false, updatable = false)
       }
)
private List<ThingCategory> thingCategory;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM