【问题标题】:Play 2.0.4 Problems updating a class that uses @EmbeddedIdPlay 2.0.4 更新使用@EmbeddedId 的类时出现问题
【发布时间】:2012-12-07 15:10:31
【问题描述】:

我最近遇到了一个带有嵌入式 id 的类的问题。每当我想更新数据库中的现有条目时,都会收到错误“java.lang.RuntimeException: No @javax.persistence.Id field found in class”。当我在已经是令人兴奋的数据库条目的对象上使用 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)
    ...

【问题讨论】:

    标签: java playframework playframework-2.0 ebean


    【解决方案1】:

    您需要在 PK 类中的键列上使用 @Id 注释。

    Ebean 还想找到一个序列来生成这些。

    【讨论】:

    • 我终于开始尝试这个,但是当我这样做时,我最终得到了这个错误[IllegalArgumentException: (was java.lang.NullPointerException) (through reference chain: model.Tag["files"]->com.avaje.ebean.common.BeanList[0]->model.File["location"])]。似乎 FileLocation 没有正确加载。
    猜你喜欢
    • 2020-04-05
    • 2021-03-13
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多