2014-10-20 68 views
0

我想在JPA 2.1中保存實體。我有三個表 - MVCollection,MVCollectionVersion(這是MVCollection的版本)和MVBelongsCollection(這是屬於一個版本的項目)。 MVCollection的主鍵是生成的序列號。 當我使用版本生成集合(沒有任何項目)時,我使用@MapsId,並且生成的ID在子內部使用。不過,我似乎無法理解如何將這些與項目複製。JPA休眠MapsId爲大孩子

以下是代碼片段至今:

@Entity 
public class MVCollection { 

    @GeneratedValue(strategy = GenerationType.SEQUENCE, 
     generator = "MVCOLLECTION_SEQ") 
    @SequenceGenerator(name = "MVCOLLECTION_SEQ", 
     sequenceName = "VMD.MVCOLLECTION_SEQ") 
    @Id 
    @Column(name = "MVCOLLECTIONID") 
    private Long id; 

MVCollectionVersion

@Entity 
public class MVCollectionVersion { 
    @EmbeddedId 
    @AttributeOverrides({ 
     @AttributeOverride(name = "versionId", column = @Column(name = "MVCVSNID")) }) 

    private MVCollectionVersionId id; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumns({ 
     @JoinColumn(name = "MVCOLLECTIONID", referencedColumnName = "MVCOLLECTIONID"), 
    }) 
    @MapsId("mvCollectionId") 
    private MVCollection mvCollection; 

    @OneToMany(fetch = FetchType.LAZY, mappedBy="mvCollectionVersion", cascade={CascadeType.MERGE, CascadeType.PERSIST}) 
    private List<MVBelongsCollection> mvCollectionItems; 

MVCollectionId

@Embeddable 
public class MVCollectionVersionId implements java.io.Serializable { 

/** 
* 
*/ 
    private static final long serialVersionUID = 2551937096790427792L; 
    private Long mvCollectionId; 
    private Integer versionId; 

MVBelongsCollection

@Entity 
public class MVCollectionItems 

    @EmbeddedId 
    @AttributeOverrides({ 
     @AttributeOverride(name = "managedViewId", column = @Column(name = "MANAGEDVIEWID")), 
     @AttributeOverride(name = "mvCollectionId", column = @Column(name = "MVCOLLECTIONID")), 
     @AttributeOverride(name = "versionId", column = @Column(name = "MVCVSNID")) }) 

    private MVBelongsCollectionId id; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumns({ 
     @JoinColumn(name = "MVCOLLECTIONID", referencedColumnName = "MVCOLLECTIONID"), 
     @JoinColumn(name = "MVCVSNID", referencedColumnName = "MVCVSNID") }) 
    private MVCollectionVersion mvCollectionVersion; 
不能將NULL插入(:

最後MVBelongsCollectionId

@Embeddable 
public class MVBelongsCollectionId implements Serializable{ 


/** 
* 
*/ 
    private static final long serialVersionUID = 1L; 

    @Column (name = "MANAGEDVIEWID", nullable = false, precision = 38) 
    private Long managedViewId; 

    @Column (name = "MVCOLLECTIONID", nullable = false, precision = 38) 
    private Long mvCollectionId; 

    @Column (name = "MVCVSNID", nullable = false, precision = 38) 
    private Integer versionId; 

,如果我嘗試創建一個集合有一個版本,並與belongsCollection項目時,創建,因爲它指出了mvCollectionId字段爲空 「ORA-01400失敗「VMD」。「MVBELONGSCOLLECTION」。「MVCOLLECTIONID」)「 因此我嘗試添加@MapsId,就像我對MVCollectionVersion所做的那樣。

public class MVBelongsCollection { 

/** 
* primary key 
*/ 
    @EmbeddedId 
    @AttributeOverrides({ 
     @AttributeOverride(name = "managedViewId", column = @Column(name = "MANAGEDVIEWID")), 
     //@AttributeOverride(name = "mvCollectionId", column = @Column(name = "MVCOLLECTIONID")), 
     @AttributeOverride(name = "versionId", column = @Column(name = "MVCVSNID")) }) 
    private MVBelongsCollectionId id; 


/** 
* collection that this joins to. 
*/ 
    @ManyToOne(fetch = FetchType.LAZY) 
    @MapsId("mvCollectionId") 
    @JoinColumns({ 
     @JoinColumn(name = "MVCOLLECTIONID", referencedColumnName = "MVCOLLECTIONID"), 
     @JoinColumn(name = "MVCVSNID", referencedColumnName = "MVCVSNID") }) 
    private MVCollectionVersion mvCollectionVersion; 

但是在Eclipse中,這顯示了

The type of the ID mapped by the relationship 'mvCollectionVersion' does not agree with the primary key class of the target entity. 

如果我啓動的過程中,我得到 所致的@ManyToOne標註錯誤:org.hibernate.MappingException:在映射重複列對於實體:MVBelongsCollection列:MVCVSNID(應映射與插入=「假」更新=「假」)

我已經嘗試向我的@JoinColumn,@AttributeOVerride和底層添加insertable = false和updatable = false ID類,但聖生病得到相同的錯誤。 只有@MapsId存在時纔會發生這種情況。

我現在不知道如何讓MVBelongsCollection使用生成的MVCollectionId,或者我如何停止eclipse和運行時錯誤。

如果有人能幫助,我將不勝感激。 在此先感謝

回答

1

,我發現我的方式錯誤...

我需要使用相同的embeddedId貫穿始終。 因此,MVBelongsCollectionId需要更改爲包含父類的嵌入ID:

@Embeddable 
public class MVBelongsCollectionId implements Serializable{ 


    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    @Embedded 
    @AttributeOverrides({ 
     @AttributeOverride(name = "mvCollectionId", column = @Column(name = "MVCOLLECTIONID", nullable = false, precision = 38, scale = 0)), 
     @AttributeOverride(name = "versionId", column = @Column(name = "MVCVSNID", nullable = false, precision = 8, scale = 0)) 

    }) 
    MVCollectionVersionId collectionVersionId; 

    @Column (name = "MANAGEDVIEWID", nullable = false, precision = 38) 
    private Long managedViewId; 

    ....