One to Many with aggregate relationship

957 views
Skip to first unread message

Angelo K. Huang

unread,
May 29, 2012, 1:14:33 AM5/29/12
to eb...@googlegroups.com

How do I specify One to Many aggregate relationship using ebean? For example,

public class Class extends Model {

    @OneToMany(cascade=CascadeType.PERSIST)

    private Set< Student > students;

}

public class Student extends Model {

}

When delete a class, the student should be not be removed. I got an error when I tried to create a student entity and save it.

[error] Test Student Test.create failed: Error inserting bean [class models.Student] with unidirectional relationship. For inserts you must use cascade save on the master bean [class models.Class].


Rob Bygrave

unread,
May 30, 2012, 5:12:55 AM5/30/12
to eb...@googlegroups.com
I don't follow this. I'd suggest you explain your question with a test case or full code example.

Markus Kahl

unread,
Jun 14, 2012, 8:43:00 AM6/14/12
to Ebean ORM
I'm having the same problem.
As an example:

@Entity
public class User extends Model {
private String name;

@OneToMany(cascade = CascadeType.PERSIST)
private List<Role> roles;

// ...
}

@Entity
public class Role extends Model {
private String name;

// ...
}

Now if I just want to create a new role like this:

new Role("admin").save()

I get the following (same) error:
Error inserting bean [class
models.Role] with unidirectional relationship. For inserts you must
use
cascade save on the master bean [class models.User].

I mean it even says the the relation is unidirectional.
So why shouldn't I be able to insert Roles indepdently?


~ Markus



On May 30, 11:12 am, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> I don't follow this. I'd suggest you explain your question with a test case
> or full code example.
>
> On 29 May 2012 17:14, Angelo K. Huang <angelohu...@gmail.com> wrote:
>
>
>
>
>
>
>
> > How do I specify One to Many aggregate relationship using ebean? For
> > example,
>
> > public class Class extends Model {
>
> >     @OneToMany(cascade=CascadeType.PERSIST)
>
> >     *private* Set< Student > students;

Rob Bygrave

unread,
Jun 14, 2012, 5:49:28 PM6/14/12
to eb...@googlegroups.com
>> So why shouldn't I be able to insert Roles indepdently?

The crux of the issue is whether you want the foreign key column to be nullable.

Typically you would make it Bi-directional and then you can choose to have that relationship nullable ... which means that null is inserted into the foreign key column.  Unidirectional in the ManyToOne case prevents that forcing the inserts to occur on a cascade from the 'master' object meaning the foreign key is always populated. So another way of looking at is ... do you have your DB tables?  If so look at the foreign key column on Role is it nullable?


Another way of looking at it is that 'unidirectional' on this direction (from the many/detail back to the one/master) implies that the many/detail bean/row can't exist without it's master. Think of an order with order lines, invoice with invoice lines ... the order lines and invoice lines are meaningless without the master.


Does that make sense?


I'm not sure about your example because typically User to Roles is a ManyToMany and not a OneToMany but I assume that the OneToMany is actually correct.



Cheers, Rob.
Reply all
Reply to author
Forward
0 new messages