0

I'm currently creating a ebean model User which trying to map back to itself. Since the User and it's child uses the data structure, my idea is to reuse them without creating a new model / table.

@Entity
public class User extends Model {

    @Id
    private String id;

    private String email;

    @ManyToOne(cascade = CascadeType.PERSIST)
    private User parent;

    @ElementCollection(fetch = FetchType.LAZY)
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<User> childs = new ArrayList<>();

    ....
}

And the database schema looks like this:

CREATE TABLE user (
id CHAR(7),
email CHAR(255),
parent_id CHAR(255),
...
);

The mapping @ManyToOne from child to parent is working fine. But i get the error below when running the app with the @OneToMany mapping introduced:

javax.persistence.PersistenceException: Error on models.User.childs  Can not find mappedBy property [user] in [models.User]

I suspect ebean was trying to look for user_id in user table, which in fact it was linked by parent_id as screen in the database structure.

Any idea if this even supported by ebean or is there anything i'm missing here?

hong823
  • 21
  • 4
  • Possible duplicate of [JPA: How to have one-to-many relation of the same Entity type](https://stackoverflow.com/questions/3393515/jpa-how-to-have-one-to-many-relation-of-the-same-entity-type) – K.Nicholas Aug 23 '18 at 00:44
  • Either you are using the JPA API, or you are using EBean. You can't use both APIs. Decide –  Aug 23 '18 at 06:21
  • An `ElementCollection` is for elements of non-entity types. Do you have that? Nope. "mappedBy" should point to a field in the element class ... so is there really a field called "user" in `User`? Nope. –  Aug 23 '18 at 06:47

0 Answers0