JPA Hibernate MapsId适合大孩子

时间:2014-10-20 18:10:52

标签: spring hibernate jpa

我正在尝试在JPA 2.1中保存实体。 我有三个表 - MVCollection,MVCollectionVersion(MVCollection的版本)和MVBelongsCollection(属于某个版本的项目)。 MVCollection的主键是生成的序列号。 当我使用版本(没有任何项目)生成集合时,我正在使用@MapsId,并且生成的ID在子项中使用。但是,我似乎无法理解如何使用这些项目来复制它。

以下是目前代码中的代码段:

@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;

最后是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字段为null &#34; ORA-01400:不能将NULL插入(&#34; VMD&#34;。&#34; MVBELONGSCOLLECTION&#34;。&#34; MVCOLLECTIONID&#34;)&#34; 因此,我尝试添加@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中,这显示了@ManyToOne Annotation of

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

如果我开始这个过程,我会得到     引起:org.hibernate.MappingException:实体映射中的重复列:MVBelongsCollection列:MVCVSNID(应使用insert =&#34映射; false&#34; update =&#34; false&#34;)

我已经尝试将insertable = false和updatable = false添加到我的@JoinColumn,@ AsttributeOVerride和底层ID类,但仍然会得到相同的错误。 这只发生在@MapsId存在时。

我现在不知道如何让MVBelongsCollection使用生成的MVCollectionId,或者我如何能够停止eclipse和运行时错误。

如果有人可以提供帮助,我将不胜感激。 提前致谢

1 个答案:

答案 0 :(得分: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;

   ....