Relating entity to more than one entity

Asked

Viewed 159 times

3

Hello, I have in my system the following entities Customer, Supplier and Financial. They all have a list of Contacts, as a bi-directional relationship would look 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. Thanks in advance.

EDIT: It would be more or less what I need, I found this example and I will study how to use why I didn’t know, but it seems to solve me the problem.

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

  • No need for annotations in the Contact class

  • I would need it because I need it to be bidirectional understood. At least I think so.

1 answer

0

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;
} 

Follows the link of the Soen who took the example, and there contains an explanation of the differences between unidirectional and bidirectional;

  • Rafael I understand the differences, I think I explained myself badly in the question. Actually, I think I need a polymorphic mapping of the Contact entity. I thought about using @Anymetadef but I never used it so I’m not sure whether to use it or not. Anyway the text is very enlightening and ended up getting other doubts of mine. I found this example (http://www.javabeat.net/polymorphic-association-mapping-relationship-hibernate/)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.