Problem in saving an entity relation ManyToOne

1,379 views
Skip to first unread message

Laurent Bois

unread,
Apr 4, 2012, 2:12:50 PM4/4/12
to play-framework
Hi

I have problems in saving an entity with ManyToOne relationship
Parent entity being "Person"

@Entity
public class Person extends Model {
...
@OneToMany
public List<Event> events;
}

Child entity (the one i save first)

@Entity
public class Event extends Model {
...
@ManyToOne(cascade = {CascadeType.ALL})
public Person person;
}

In my controller, a "save" action does the following :

Event event = new Event();

..

Person person = Person.findByUsername(username);

event.person = person;

event.save();

And in the view *.scala.html , i display like this .... and i do not
see the person username displayed on the page.

@event.description (@event.level) @event.date @event.person.pseudonym

As i have no db manager (the following URL http://localhost:9000/@db
doesn't work ) , i could not verify if the person id of my table
"events" is filled with a value

Any idea is welcome

Thanks for your help

Laurent

Nicolas Forney

unread,
Apr 5, 2012, 3:53:14 AM4/5/12
to play-fr...@googlegroups.com
Hi Laurent,

Are you sure Person.findByUsername(username) return a user? Have you try to debug?

By the way replace the @OneToMany annotation of your events attribute with the following one  :
@OneToMany(mappedBy="category", cascade=CascadeType.ALL) 

It will ensure User will be the owner of the Event. A foreign key will be created in the User table.

See you


Nicolas

@OneToMany(mappedBy="person", cascade=CascadeType.ALL)

Laurent Bois

unread,
Apr 5, 2012, 4:14:32 AM4/5/12
to play-fr...@googlegroups.com
Nicolas thanks for your answer

I've added in my Person entity , @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
My problems comes from the Event entity having a @ManyToOne(cascade=CascadeType.ALL) annotation for the property "Person person" ;

I wrote a unit test, but it's still failing with the error : 
[error] Test models.EventTest.saveEvent failed: expected:<LOLO> but was:<null> on the line "assertEquals("LOLO", e.person.pseudonym);"

here is the code of the test :

@Test

public void saveEvent() {

//We assume Event is linked to a person

// 1rst save a new Person

Person person = new Person();

person.firstName = "Laurent";

person.lastName = "Bois";

person.pseudonym = "LOLO";

person.save();

assertNotNull(person.id);

Person p = Person.findByPseudonym("LOLO");

assertEquals("Laurent", p.firstName);

assertEquals("Bois", p.lastName);

assertEquals("LOLO", p.pseudonym);

Event event = new Event();

event.code = "HRF";

event.description = "Test unitaire saveEvent";

event.level = new Long(0);

event.person = p;

event.save();

assertNotNull(event.id);

// Find the event 

Event e = Event.findByDescription("Test unitaire saveEvent");

assertEquals("HRF", e.code);

assertEquals(new Long(0), e.level);

assertEquals("LOLO", e.person.pseudonym);

assertEquals("Laurent", e.person.firstName);

assertEquals("Bois", e.person.lastName);

}

The Cascade saving for event.person is not done .

Laurent

Laurent Bois

unread,
Apr 5, 2012, 4:39:13 AM4/5/12
to play-fr...@googlegroups.com
I have the same problem between 2 entities, each having the annotation @OneToOne(cascade=CascadeType.ALL) on a property (bi directionnal)
I could not save this relationship Address-Person in my unit test :-(

Pascal Voitot Dev

unread,
Apr 5, 2012, 6:48:18 AM4/5/12
to play-fr...@googlegroups.com
I'm not ebean expert but I don't see why it wouldn't work :)
If you try the following, does it behave differently?

Add the event to the list in person and save:

person.events.add(event);
person.save

Event e = Event.findByXXX("XXX");
assert(e.person != null);

Another thing: when you change the relation, do you re-generate the DB schema because I think some foreign keys might be created with "mappedBy" for ex.

pascal

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/jjSNQSYdvuoJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Leonard Punt

unread,
Apr 5, 2012, 7:03:07 AM4/5/12
to play-fr...@googlegroups.com
To access your h2 database, execute the 'h2-browser' command from the play console. After that you can access your browser on http://localhost:8082/.

Also, I think your problem is the (lazy) loading of the data, not the persisting of the data.

Op woensdag 4 april 2012 20:12:50 UTC+2 schreef Laurent Bois het volgende:

Laurent Bois

unread,
Apr 5, 2012, 7:09:20 AM4/5/12
to play-fr...@googlegroups.com
Pascal ,

Thanks for your answer, i just created this unit test as you asked for... and it does not work either in the direction OneToMany ( person -> events ).

My problem still exist in the direction ManyToOne (from the Event -> Person) and for OneToOne (Person <-> Address) :-(

Yes i clean recompile before starting then recreate the DB schema , after changed relationships in the domain model classes.

Regards

Laurent

To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Laurent Bois

unread,
Apr 5, 2012, 7:56:46 AM4/5/12
to play-fr...@googlegroups.com
Pascal,

I wrote this Unit Test 

Event event1 = new Event();

event1.code = "HRF";

event1.description = "Test unitaire saveEvent INFO";

event1.level = new Long(0);

Event event2 = new Event();

event2.code = "HRF";

event2.description = "Test unitaire saveEvent WARNING";

event2.level = new Long(1);

Event event3 = new Event();

event3.code = "HRF";

event3.description = "Test unitaire saveEvent ALARM";

event3.level = new Long(2);

    List<Event> events = new ArrayList<Event>();

    events.add(event1);

    events.add(event2);

    events.add(event3);

     

    Person person = new Person();

person.firstName = "Laurent";

person.lastName = "Bois";

person.pseudonym = "LOLO";

person.events = events;

person.save();

assertNotNull(person.id);

Person p = Person.findByPseudonym("LOLO");

assertNotNull(p.events);

assertEquals(3, p.events.size());


Event e3 = Event.findByDescription("Test unitaire saveEvent ALARM");

assertNotNull(e3);

assertEquals("Test unitaire saveEvent ALARM", e3.description);

Event e2 = Event.findByDescription("Test unitaire saveEvent WARNING");

assertNotNull(e2);

assertEquals("Test unitaire saveEvent WARNING", e2.description);

Event e = Event.findByDescription("Test unitaire saveEvent INFO");

assertNotNull(e);

assertEquals("Test unitaire saveEvent INFO", e.description);

assertEquals("HRF", e.code);

assertEquals(new Long(0), e.level);

assertNotNull(e.person);

assertEquals("Laurent", e.person.firstName);

assertEquals("Bois", e.person.lastName);

assertEquals("LOLO", e.person.pseudonym);


I check everything has been save, and that i can retrieve the 3 Event objects from the Person
The problem is finally on this assertion : assertEquals("Laurent", e.person.firstName);
I get a null while expecting "Laurent"...
It seems the Lazy Loading doesn't work

Laurent

Le mercredi 4 avril 2012 20:12:50 UTC+2, Laurent Bois a écrit :

Pascal Voitot Dev

unread,
Apr 5, 2012, 8:22:53 AM4/5/12
to play-fr...@googlegroups.com
http://www.avaje.org/lazyexample.html

Apparently, getters/setters are intercepted by ebean to lazy load entities...

Pascal

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/ARSAVPGJ7zkJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages