java – Relate entity to more than one entity

Question:

Hi, I have the following entities Customer, Supplier and Financial in my system. All of them have a list of Contacts, as a bidirectional relationship would look like in these molds.

public class Client {
    @OneToMany
    private List<Contact> contacts
}

public class Supplier {
    @OneToMany
    private List<Contatc> contacts
}

public class Financial {
    @OneToMany
    private List<Contat> contacts
}

public class Contact {
    @ManyToOne
    ?????????
}

I'm using Hibernate as ORM and SpringMVC. Thank you very much in advance.

EDIT: That would be more or less what I need, I found this example and I'm going to study how to use it because I didn't know it, but it seems to solve the problem for me.

http://www.javabeat.net/polymorphic-association-mapping-relationship-hibernate/

Answer:

Here is an example of bidirectional:

public class User {
    private int     id;
    private String  name;
    @ManyToOne
    @JoinColumn(
            name = "groupId")
    private Group   group;
}
public class Group {
    private int         id;
    private String      name;
    @OneToMany(mappedBy="group")
    private List<User>  users;
} 

Here's the SOen link I took the example, and there it contains an explanation about the differences between unidirectional and bidirectional;

Scroll to Top