title: How to specify column names in Hibernate/JPA when inheritance is involved?
body: <p>I think I'm trying to have my cake and eat it too here, but we'll see if there's a plausible solution to what I'm looking for. I have a Spring Boot/JPA/Hibernate app that will be talking to MySQL as its backing store. I have several cases where my entity classes form, from an OOP perspective, a parent/child hierarchy like so:</p>
  
  <pre><code>// Groovy pseudo-code!
  class Vehicle {
    Long id
    Long maxSpeed
    String make
    String model
  }
  
  class Motorcycle extends Vehicle {
    Boolean isTwoStroke
  }
  
  class Car extends Vehicle {
    Boolean hasLeatherInterior
  }
  </code></pre>
  
  <p>etc. Normally, outside of JPA, I might design their respective tables like so:</p>
  
  <pre><code>CREATE TABLE motorcycles (
    motorcycle_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    motorcycle_max_speed BIGINT UNSIGNED,
    motorcycle_make VARCHAR(50) NOT NULL,
    motorcycle_model VARCHAR(50) NOT NULL,
    motorcycle_is_two_speed BIT NOT NULL,
  
    # PK, FK, UC, index constraints down here (omitted for brevity)
  );
  
  CREATE TABLE cars (
    car_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    car_max_speed BIGINT UNSIGNED,
    car_make VARCHAR(50) NOT NULL,
    car_model VARCHAR(50) NOT NULL,
    car_has_leather_interior BIT NOT NULL,
  
    # PK, FK, UC, index constraints down here (omitted for brevity)
  );
  </code></pre>
  
  <p><strong>Ideally</strong> I'd like to keep this table design as-is, with the names of the "parent vehicle" columns staying exactly as I have the, above. But if I'm understanding the Hibernate/JPA APIs correctly, then I don't <em>think</em> its possible without making some sort of sacrifice. I think I either need to sacrifice inheritance at the app-layer so that I can name the columns in the child classes exactly as I have them in the DB:</p>
  
  <pre><code>@Entity
  class Motorcycle {    // No longer extends Vehicle :-(
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "motorcycle_id")
    Long id
  
    @Column(name = "motorcycle_max_speed")
    Long maxSpeed
  
    @Column(name = "motorcycle_make")
    String make
  
    @Column(name = "motorcycle_model")
    String model
  
    @Column(name = "motorcycle_is_two_speed")
    Boolean isTwoStroke
  }
  
  @Entity
  class Car {    // No longer extends Vehicle :-(
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "car_id")
    Long id
  
    @Column(name = "car_max_speed")
    Long maxSpeed
  
    @Column(name = "car_make")
    String make
  
    @Column(name = "car_model")
    String model
  
    @Column(name = "car_has_leather_interior")
    Boolean hasLeatherInterior
  }
  </code></pre>
  
  <p><strong>Or</strong> I think I can keep app-layer inheritance, but then would need to refactor my DB tables like so:</p>
  
  <pre><code>CREATE TABLE motorcycles (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    max_speed BIGINT UNSIGNED,
    make VARCHAR(50) NOT NULL,
    model VARCHAR(50) NOT NULL,
    motorcycle_is_two_speed BIT NOT NULL,
  
    # PK, FK, UC, index constraints down here (omitted for brevity)
  );
  
  CREATE TABLE cars (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    max_speed BIGINT UNSIGNED,
    make VARCHAR(50) NOT NULL,
    model VARCHAR(50) NOT NULL,
    car_has_leather_interior BIT NOT NULL,
  
    # PK, FK, UC, index constraints down here (omitted for brevity)
  );
  </code></pre>
  
  <p>So I ask: is it possible for me to keep my app-layer inheritance (and have <code>Motorcycle</code> and <code>Car</code> inherit those properties from <code>Vehicle</code>) <strong>and</strong> keep my DB table columns named using my preferred convention?</p>