views:

461

answers:

1

Hi,

I'm getting started with JPA and created the following (simplified) tables;

airport
--------
id (pk)
city
code
...

trip
--------
id (pk)
originAirport_id  (fk)
destinationAirport_id  (fk)
...

I'm trying to map this in JPA/Hibernate.

In My trip object I created 2 airport objects:

private Airport airportFrom;
private Airport airportTo;

And I annotated the getters like this:

    @ManyToOne
public Airport getAirportFrom() {
 return airportFrom;
}

    @ManyToOne
public Airport getAirportTo() {
 return airportTo;
}

In my Airport class I created a HashSet of 'trip' objects

    private Set <Trip> trips = new HashSet<Trip>();

And annotated the getter:

    @OneToMany (mappedBy="airport")
public Set <Trip> getTrips() {
 return trips;
}

This would be simple to implement in SQL but I don't know how to annotate and implement that relationship in JPA/Hibernate.

Any tips?

+1  A: 

I believe you missed a little detail. In the Airport class, the Set<Trip> trips you have must correspond to what is in the other table. I would suggest:

    public class Airport {
       private Set<Trip> fromTrips = new HashSet<Trip>();
       private Set<Trip> toTrips = new HashSet<Trip>();

       @OneToMany (mappedBy="airportFrom")
       public Set <Trip> getFromTrips() {
          return fromTrips;
       }
       @OneToMany (mappedBy="airportTo")
       public Set <Trip> getToTrips() {
          return fromTrips;
       }

    }

The "mappedBy" must point toward the name of a property in the Many class. Here, the two properties in Trip are airportFrom and airportTo.

So you have two Sets of Trips, the fromTrips and the toTrips. I hope you are ok with this?


Edited, after an excellent comment by ChssPly76, that would deserve to be an answer of its own. I would vote for it :-)

If the association needs to be bi-directional that's the way to map it. I'm not sure it has to be, though - Airport has no business dealing with collections of trips. Consider making the association uni-directional instead and using a query when / if you need to find all the trips taken from / to airport in question (you'll be able to get a single list this way too).

KLE
+1. If the association needs to be bi-directional that's the way to map it. I'm not sure it has to be, though - Airport has no business dealing with collections of trips. Consider making the association uni-directional instead and using a query when / if you need to find all the trips taken from / to airport in question (you'll be able to get a single list this way too)
ChssPly76
@ChssPly76 excellent comment, I agree. I included your comment in my answer. If you want to make it a separate answer, I think it deserves it, I will take it away. I voted +1 for you on a different answer anyway, you deserve it.
KLE
The solution offered by KLE works, although as well noted by ChssPly76 I shouldn't need it. While reading one Hibernate book I found the note 'One of the most common mistakes hibernate beginners do is try do use bi-direccional mappings everywhere.', which was exactly what I was doing.The terms 'MappedBy', 'Inverse' and 'Owning' entity are also a great source of confusion (as I found out reading several contradictory posts elsewhere).Long story short, the 'MappedBy' annotation should go in the 'One'side, the one that doesn't have the Foreign keys in the DB. Thanks!
Cleber Goncalves