代码之家  ›  专栏  ›  技术社区  ›  Pradyut Bhattacharya

在JPA中使实体类行为只读

  •  0
  • Pradyut Bhattacharya  · 技术社区  · 14 年前

    当我只映射了一个表时,我可以使用JPA获取值

    现在,当我去映射相关的表时,我得到了一个错误

      Only one may be defined as writable, all others must be specified read-only.
      Mapping: org.eclipse.persistence.mappings.OneToOneMapping[userId1]
    

    我正在使用[EclipseLink-0](Eclipse持久性服务-2.0.1.v20100213-r6600)

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "albumId")
    private Collection<Images> imagesCollection;
    @JoinColumn(name = "user_id", referencedColumnName = "User_ID")
    @ManyToOne(optional = false)
    private Roleuser userId;
    @JoinColumn(name = "album_image", referencedColumnName = "image_id")
    @ManyToOne
    private Images albumImage;
    @JoinColumn(name = "album_image", referencedColumnName = "image_id")
    @ManyToOne
    private Images albumImage1;
    @JoinColumn(name = "user_id", referencedColumnName = "User_ID")
    @ManyToOne(optional = false)
    private Roleuser userId1;
    

    要使字段为只读,我必须在代码中做哪些更改?

    1 回复  |  直到 14 年前
        1
  •  1
  •   James    14 年前

    您有两个使用同一外键字段的多对一关系。这没有道理,他们会是同一个物体吗?

    @JoinColumn(name = "album_image", referencedColumnName = "image_id")
    @ManyToOne
    private Images albumImage;
    @JoinColumn(name = "album_image", referencedColumnName = "image_id")
    @ManyToOne
    private Images albumImage1;
    

    您应该有两个不同的外键字段,即albumImage和albumImage1。