Save a model with a relation OneToMany

137 views
Skip to first unread message

Bouki

unread,
May 16, 2011, 3:50:20 AM5/16/11
to play-framework
Hi
I have models like this :

public class Person extends Model {
...
@OneToMany(orphanRemoval = true, mappedBy = "person")
@Cascade({ CascadeType.ALL })
public List<Contact> infos = new ArrayList<Contact>();
}

public class Contact extends Model {
...
@ManyToOne(optional = false)
public Person person;
}

And I have a method in my controller like this :

public static void savePerson(Person person) {
person.save();
renderJSON(person);
}

My issue is that when I try to save a person with savePerson() I have
this error (only if my list of Person isn't empty) :

PersistenceException occured : org.hibernate.HibernateException: A
collection with cascade="all-delete-orphan" was no longer referenced
by the owning entity instance: models.Person.infos

I don't understand the error message because it appears if the list
was previously empty or not.

Nicolas

unread,
May 17, 2011, 3:34:45 PM5/17/11
to play-fr...@googlegroups.com
Weird, I don't seem to be able to reproduce this problem. Which Play! version are you using?

Bouki

unread,
May 19, 2011, 9:43:15 AM5/19/11
to play-framework
I'm using 1.2.1

Bouki

unread,
May 20, 2011, 8:55:57 AM5/20/11
to play-framework
I'll describe more precisely the issue.

This is the original error I have on any insert :

java.lang.RuntimeException: java.util.concurrent.ExecutionException:
play.exceptions.JavaExecutionException:
org.hibernate.PropertyValueException: not-null property references a
null or transient value: models.Contact.person

I fixed it manually by overriding the method save from class Person
like :
for (Contact contact : this.infos) {
contact.person = this;
}
super.save();

Then this works partially, I have this error when infos is filled

PersistenceException occured : org.hibernate.HibernateException: A
collection with cascade="all-delete-orphan" was no longer referenced
by the owning entity instance: models.Person.infos

Can anyone help me to solve this strange issue ?

Bouki

unread,
May 23, 2011, 3:40:04 AM5/23/11
to play-framework
anyone has an idea please ?

On 20 mai, 14:55, Bouki <jonathan.boko...@gmail.com> wrote:
> I'll describe more precisely the issue.
>
> This is the original error I have on any insert :
>
> java.lang.RuntimeException: java.util.concurrent.ExecutionException:
> play.exceptions.JavaExecutionException:
> org.hibernate.PropertyValueException: not-null property references a
> null or transient value: models.Contact.person
>
> I fixed it manually by overriding the methodsavefrom class Person

Xuefeng Wu

unread,
May 23, 2011, 4:49:02 AM5/23/11
to play-fr...@googlegroups.com
Do you want save contact when you call savePerson?

Bouki

unread,
May 23, 2011, 6:52:06 AM5/23/11
to play-framework
yes
Reply all
Reply to author
Forward
0 new messages