0

I have a problem with JPA inheritance. See my entities below. I have a Person that can be in either a House or a Car, never at the same time of course. Both Car and House implement the PersonHoldable interface. I know I cannot map an Entity directly to an interface.

This is my model:

@Entity
public class Person{
  private PersonHoldable personHoldable; // either a Car or a House

  // This does not work of course because it's an interface
  // This would be the way to link objects without taking JPA into consideration.
  @OneToOne 
  public PersonHoldable getPersonHoldable() {
    return this.personHoldable;
  }

  public void setPersonHoldable(PersonHoldable personHoldable) {
    this.personHoldable = personHoldable;
  }
}

@Entity
public class Car implements PersonHoldable{}

@Entity
public class House implements PersonHoldable{}

public interface PersonHoldable{}

How can I map this correctly in JPA taking the following into consideration?

  1. I tried @MappedSuperclass on an abstract implementation of PersonHoldable. Although it will work for this particular setup, the problem with this is that Car and House in reality implement more interfaces. And they are mapped to other entities as well.
  2. The Person could have a property for every possible PersonHoldable, so in this case it could have a getCar() and getHouse() property. That does not seem very flexible to me. If I would add a Bike implementation of the PersonHoldable I would have to change my Person class.
  3. I can map the other way around, so having a OneToOne relation only on the PersonHoldable implementation side. This would mean adding a getPerson() property to the PersonHoldable. But then it's not very easy from a Person perspective to see what PersonHoldable it is linked to.
  4. I'm using default JPA, so no Hibernate specific tags if possible.

If this is not possible with default JPA, what would be best practice in this case?

6
  • 2
    This is not supported by standard JPA. It would need a @Any hibernate mapping to map this kind of association.
    – JB Nizet
    Jun 4, 2013 at 9:04
  • Thanks, seems the Hibernate @Any is nothing more than just two columns, one containing the type and one containing an id. Could be implemented manually. Is that common practice? Jun 4, 2013 at 9:17
  • 3
    It's "nothing more", except it generats all the appropriate queries for you, like select p from Person p left join fetch p.personHoldable for example, and it allows navigating to the personHoldable without caring what type it is. Is it common practice to use such a mapping? I've never had to use it myself. I'd rather define an abstract entity that all the PersonHoldable entities would extend. That would be standard JPA.
    – JB Nizet
    Jun 4, 2013 at 9:29
  • @JBNizet so you basically suggest to use point 1 in my question, with the @MappedSuperclass. Right? Jun 4, 2013 at 11:12
  • 1
    No. You can't have an association to a MappedSuperclass. Only to entities. What I suggest (but remember that I only have a partial view of the problem) is to use entity inheritance: an abstract PersonHoldable entity, having sub-entities (Car, House, etc.)
    – JB Nizet
    Jun 4, 2013 at 11:27

1 Answer 1

1

A slight variation on your second point would be to make Person have an inheritance type and implement a CarPerson and HousePerson (and later a BikePerson) whose whole purpose is to define the specific join relationship to a specific PersonHolder implementation. That keeps the relationship intact and more easily queryable from the Person side.

@Inheritance(strategy = JOINED)
@DiscriminatorColumn(name="holdableType", discriminatorType=CHAR, length=1)
@Entity
public class Person {
    // common fields
}

@Entity
@DiscriminatorValue("C")
public class CarPerson extends Person {
    @OneToOne
    private Car car;
}

@Entity
@DiscriminatorValue("H")
public class HousePerson extends Person {
    @OneToOne
    private House house;
}
5
  • This makes it impossible to change the place where it lives, though. Once it's a CarPerson, it's a CarPerson and can't become a HousePerson. I'd rather use inheritance for the PersonHoldable side.
    – JB Nizet
    Jun 4, 2013 at 10:22
  • Once it's a CarPerson, it has data associated with it in a car-related table. The inability to quickly transfer to a HousePerson is a factor of that, and not the object's hierarchy. If there is a business need to move between being a CarPerson and a HousePerson, that can easily be accomplished with a factory method that handles the persistence layer appropriately. I'd argue that someone could be both a car and house person at the same time, but the question as written clearly indicates a 1-1 relationship.
    – Jeff
    Jun 4, 2013 at 10:28
  • the problem is not related to the fact that they're stored in different tables. Even with single-table inheritance, you would have this problem, which is caused by the fact that an object of a given type can't become an object of another type in Java. So the only ways to change the type is to delete the person and recreate it as another type (doesn't work if there are foreign keys to the person), or to use JDBC to move the row from one table to the other, or to change the discriminator value in the row in case of single-table inheritance.
    – JB Nizet
    Jun 4, 2013 at 10:31
  • I'm not suggesting that the problem is they're stored in different tables because I used a joined table inheritance, not a table per subclass. The only thing that is in a different table is the car vs. house vs. bike data. Why would you use inheritance on the side of the relationship where things have potentially nothing in common (bike, car, house) instead of on the side of the relationship where the only difference is what the person holds?
    – Jeff
    Jun 4, 2013 at 10:37
  • They all have something in common: they hold a person. Having inheritance on the other side is also much closer to what the OP wants: replace the interface by an abstract entity, and you have my suggested solution. I'm not saying it's the right one, because I don't know the problem in details, and I doubt the entities are really car, house and bike. And your solution might be cleaner. I'm just pointing out that if the goal is to change the location of the person at runtime, it's not appropriate.
    – JB Nizet
    Jun 4, 2013 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.