当我仅映射一个表时,我可以使用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 Persistence Services-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;


我必须在此代码中进行哪些更改才能使字段为只读?

还是有一些更好的解决方法,可以不将这些字段设为只读?

最佳答案

您有两个使用相同外键字段的ManyToOne关系。这没有道理,它们将是同一对象吗?

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


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

09-05 20:05