Hoffmann
Hoffmann

Reputation: 14719

How to map a many-to-many relantionship in OrientDB

I'm starting with OrientDB and I got the basics right. But I can't figured out how to map many-to-many relationships in a way that won't require me to make tons of integrity checking when removing a record.

I am using an object database (ODatabaseObjectTx) and I'm trying to map this simple relationship:

Person <-> Roles

A Person can have multiple Roles and a Role can be assigned to multiple Persons. N to M relationship.

My person class:

public class Person {
    private String name;

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My Role class:

public class Role {
    private String name;

    public Role() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Now I can't figure out if I have to put a Set<Role> in the User class or a Set<User> in the Role class to make the links. Either way if I remove one record from one I have to check the links of the other to avoid null references. To me it is more convenient to place a Set<Role> in the User class.

In a RMDBS you can just create a foreign key and add some cascading rules, does OrientDB provide similar functionality to save me the bother of doing this manually?

If in the end I have to do this manually, what strategy should I take? Should I fully check the other cluster when removing a record? Should I just leave the nulls and handle them when I see them? Should I create double references (having both Set<Role> in User AND Set<User> in Role, I guess this would bring faster removal at the cost of more space and complexity)?

Upvotes: 4

Views: 855

Answers (1)

Related Questions