PersistenceException来自简单类的简单CRUD

时间:2011-03-24 12:56:51

标签: jpa crud playframework

使用内存数据库(db = mem),我只是尝试使用CRUD将新对象插入数据库。

课程如下:

@Entity
public class ActivityModel extends Model {
    @Required
    public String description;

    @Required
    public String link;

    @Required
    public Date timestamp;

    public ActivityModel(String description, String link, Date timestamp) {
        this.description = description;
        this.link = link;
        this.timestamp = timestamp;
    }
}

CRUD控制器看起来像这样:

@CRUD.For(ActivityModel.class)
public class Activities extends CRUD {
}

然而,当我尝试向数据库添加新活动时,我收到以下错误:

13:48:20,710 INFO  ~ Application 'ScoreDB' is now started !
13:48:48,323 WARN  ~ SQL Error: -177, SQLState: 23000
13:48:48,323 ERROR ~ Integrity constraint violation - no parent FKF0DCD93AADF686FF table: USERMODEL in statement [insert into ActivityModel (id, description, link, timestamp) values (null, ?, ?, ?)]
13:48:48,370 ERROR ~ 

@65pagp52g
Internal Server Error (500) for request POST /admin/activities

Execution exception (In {module:crud}/app/controllers/CRUD.java around line 135)
PersistenceException occured : org.hibernate.exception.ConstraintViolationException: could not insert: [models.users.hidden.ActivityModel]

play.exceptions.JavaExecutionException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.users.hidden.ActivityModel]
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:290)
    at Invocation.HTTP Request(Play!)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.users.hidden.ActivityModel]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1235)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1174)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:674)
    at play.db.jpa.JPABase._save(JPABase.java:37)
    at controllers.CRUD.create(CRUD.java:135)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:413)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:408)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:182)
    ... 1 more

... etc ...

我做错了什么? :S

1 个答案:

答案 0 :(得分:1)

显然这是因为有一个不同的类包含以下代码:

@OneToMany(cascade = CascadeType.ALL, mappedBy="id")
public Collection<ActivityModel> activities;

其中'mappedBy =“id”'是问题的根源。

删除它,一切正常:)