Hibernate: Multiple FK relationships

I'm kinda stuck defining a Hibernate Entity:

Assuming I have the following two tables in a database:


  • fromCompany
  • toCompany
  • viaCompany


  • companyID (PK)
  • description

where the elements of (A) point to the primary key of (B); so there are 3 one-to-one relationships between the FKs and the PK. I assume 3 OneToOne statements with different mappedBy conditions are not the way to go, mh? Has been a long day - I probably just don't get it ;)

Thanks for your help!


If I understood correctly, you want your database to look like this:

- id
- fromCompanyId (references tableB.id)
- toCompanyId (references tableB.id)
- viaCompanyId (references tableB.id)

- id
- description

If so, you can certainly have this. You just need to override the default column name for the relationship, so that each association have it's own column name instead of falling back to the default name, which would render the three associations with the same name, causing problems.

I don't really agree with OneToOne, but I assume this was a conscious decision.

See this: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-association

You certainly can do this with one-to-one relationships. You would have three Company objects in your A class, and appropriate mappings.

It would help if we could see the code and the hibernate mappings.

The easiest way to implement Class A with 3 ManyToOne Relationships pointing to ClassB

class ClassA {

    //private id;

    private ClassB from;

    private ClassB toCompany;

    private ClassB viaCompany;

would not work, because every entity needs an Id. – You could try to overcome this problem by using a combinded Id (consisting of the three associations), but I guess it results in a lot of trouble to implement a ID consisting of associations to other entities.

@see Hibernate/persistence without @Id

The other way provided by Hibernate are so called Ternary Associations. – In this case you model the complete table A in form of an ternary m:n relationship. – But this is a very complicated task too.

@see http://docs.jboss.org/hibernate/core/3.5/reference/en/html/collections.html#collections-ternary

Need Your Help

Getting file from URL - error

java url httpconnection polarion

Thanks to this question( link ), I know how to download a file from the internet. However, instead of a normal Text in a txt-file i get a html response. Does anyone know what I'am doing wrong?

How to save Editable div content in local storage.

javascript html

I am try to make small application there i have some editable dives so now i need to make setup if user refresh page than editable dives content store in local storage via JavaScript and update local

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.