Test and OneToMany / ManyToOne

27 views
Skip to first unread message

xael-fry

unread,
Dec 29, 2011, 9:15:53 PM12/29/11
to play-framework
I write a simple test but I have got an arrset error (nature.sets =
null)


@Entity
@Table(name = "natures")
public class Nature extends Model {
@Required
@MaxSize(64)
@Column(length = 64)
public String name;


@OneToMany(mappedBy = "companyNature", fetch = FetchType.LAZY,
cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REMOVE })
public List<Set> sets;

}


@Entity
@Table(name = "sets")
public class Set extends Model {

/**
* The name for the set. Permits to have significative info in error
messages.
*/
@Required
@MaxSize(64)
@Column(length = 64)
public String name;


/**
* If this set is associated with a company nature it will be
contained in
* that list.
*/
@ManyToOne(fetch = FetchType.LAZY)
public Nature Nature;

}



@Test
public void testDeleteNature() {

Nature c = newNature();
c.name = "Gambling2";
c.save();

Set set = new Set();
set.name = "test";
set.nature = c;
set.save();


Nature nature = Nature.find("byName", "Gambling2").first();

assertNotNull(nature );
assertNotNull(nature.sets);
assertTrue(nature .sets.size() > 0);

Does somebody has some idea about the problem ?



jhice

unread,
Dec 30, 2011, 8:57:51 AM12/30/11
to play-framework
Did something like this but with a join table.

Anyway, your "sets" being in "Nature", perhaps you should write
something like this, in "Test" :

c.addSet(set); // instead of set.nature = c;

And in "Nature" :

public static void addSet(Set set)
{
this.sets.add(set); // dont remember if it's the right syntax for
List to add
this.save();
}

I've got a code like this that works.
Reply all
Reply to author
Forward
0 new messages