pplcf
pplcf

Reputation: 43

How to do unidirectional one-to-many relationship on the same entity class?

How to do unidirectional one-to-many relationship on the same entity class?

@Entity
public class User extends Model {
    @Id
    private Long id;
    ....
    @OneToMany(cascade = CascadeType.PERSIST)
    @JoinTable(name="ignores",
               joinColumns = @JoinColumn(name = "user_id"),
               inverseJoinColumns = @JoinColumn(name="ignored_id"))
    public List<User> ignoreList;
    ....        
}

[PersistenceException: Error inserting bean [class models.User] with unidirectional relationship. For inserts you must use cascade save on the master bean [class models.User].]

Upvotes: 1

Views: 2035

Answers (2)

nazareno
nazareno

Reputation: 136

As far as the documentation goes, it seems you cannot. On the one hand, you must persist the master bean; on the other, the master bean may itself be contained on a master bean.

I am currently having a similar problem, and as far as I could determine, this is an Ebean limitation.

Upvotes: 2

d1e
d1e

Reputation: 6452

It basically says, that for INSERTS you must use CASCADE SAVE on MASTER BEAN.

You should persist parent User, which holds ignored users.

Upvotes: 0

Related Questions