0

I'm kinda stuck defining a Hibernate Entity:

Assuming I have the following two tables in a database:

(A)

  • fromCompany
  • toCompany
  • viaCompany

(B)

  • 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!

henning
  • 3
  • 1
  • 1
    Is this definitely OneToOne and not ManyToOne? If table A is lets say a shipment table, can a company in table B only ship one item? – Qwerky Nov 30 '10 at 15:40

3 Answers3

0

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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

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

@Entity
class ClassA {

    //@ID
    //private id;

    @ManyToOne
    private ClassB from;

    @ManyToOne
    private ClassB toCompany;

    @ManyToOne
    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

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
0

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

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

tableB
- 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

jpkroehling
  • 13,881
  • 1
  • 37
  • 39
  • Thanks so much guys - now some hours of sleep later, I regained my abilities to think ;) and feel a bit sorry for bugging you. – henning Dec 01 '10 at 10:06
  • The table structure is indeed as you thought. Yes, the idea of a One-To-One relationship comes from the application scenario. Although there is a chance, that the same ID is used by eg. fromCompany and viaCompany, I tend to see these as two semantically different objects, because the company itself acts in a different role in each data field...hard to describe w/o explaining the whole thing. But you are certainly right, that in general a One-to-Many relation might be the better way. – henning Dec 01 '10 at 10:27