开发者

PersistenceException from a simple CRUD for a simple class

Using the in-memory database (db=mem), I'm simply trying to insert a new object into the database using CRUD.

The class is as follows:

@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;
    }
}

And the CRUD controllers looks like this:

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

Yet, when I try to add a new activity to the database, I get the following error:

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 ...

What am I doing wrong? :S


Apparently it was because there was a different class containing the following code:

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

In which the 'mappedBy="id"' is the source of the problem.

Remove this and it all works :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜