我最近遇到了一个带有嵌入式 id 的类的问题。每当我想要更新数据库中的现有条目时,都会收到错误“java.lang.RuntimeException:在类中找不到@javax.persistence.Id字段
”。仅当我在已经是令人兴奋的数据库条目的对象上使用 update()
或 save()
时,才会出现此错误。使用 save()
插入新条目可以正常工作,使用 delete()
删除现有条目也可以。
其他人在 Play Framework Google Group 中发布了有关此问题的问题,但遗憾的是从未得到答复。所以我想我应该尝试在这里寻求帮助。
这是我的代码的基本外观:
@Entity
@Table(name = "files_location")
public class FilesLocation extends Model {
@EmbeddedId
public FilesLocationPK ids;
@Column(name="status")
public Character status;
@ManyToOne
@MapsId("fileId")
@JoinColumn(name = "file_id", referencedColumnName = "id", insertable = false, updatable = false)
public File file;
@ManyToOne
@MapsId("locationId")
@JoinColumn(name = "location_id", referencedColumnName = "id", insertable = false, updatable = false)
public Location location;
}
@Embeddable
public class FilesLocationPK {
@Column(name="file_id")
public Integer fileId;
@Column(name="location_id")
public Integer locationId;
...
}
错误看起来像这样:
java.lang.RuntimeException: No @javax.persistence.Id field found in class [class models.FilesLocation]
at play.db.ebean.Model._idAccessors(Model.java:39)
at play.db.ebean.Model._getId(Model.java:52)
at play.db.ebean.Model.hashCode(Model.java:183)
at java.lang.Object.toString(Object.java:219)
at java.text.MessageFormat.subformat(Unknown Source)
at java.text.MessageFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at java.text.MessageFormat.format(Unknown Source)
at com.avaje.ebeaninternal.server.core.Message.msg(Message.java:39)
...
最佳答案
您需要在 PK 类中的关键列上使用 @Id 注释。
Ebean 也希望找到一个序列来生成这些序列。
关于java - Play 2.0.4 更新使用 @EmbeddedId 的类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13765775/