在JPA中使实体类行只读

时间:2010-08-25 11:14:00

标签: java orm jpa entity-relationship eclipselink

当我只映射了一个表

时,我能够使用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;

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

或者有没有更好的解决方法,不让这些字段只读?

1 个答案:

答案 0 :(得分:1)

您有两个使用相同外键字段的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。