Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism with JPA and Hibernate

 
Bennett Smithsonian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using JPA for a project, and something has me stumped. Here's the scenario:

I have an abstract class called Automobile:



There are two concrete classes that subclass Automobile:





Using Hibernate's hbm2ddl, I get two tables -- one called Car, one called Truck. That is what I would expect.

However, I am now trying to introduce an object called Parking Lot, which has a Collection of Automobiles (which could be either Cars or Trucks). Here's that class:



When hbm2ddl sees this, it creates a table for Parking Lot, like I'd expect. It also creates a table that would hold the Parking Lot/Automobile relationship, which it calls ParkingLot_Automobile. However, this table has two columns: one called ParkingLot_id, and one called automobile_id. There isn't a column for the discriminator, which I thought I set up by adding the @DiscriminatorColumn annotation to Automobile. Anyone know what I'm doing wrong? I haven't been able to find any documentation on this. Thanks in advance...
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried adding records to both tables, then look at the ids that are generated for them. Do you notice anything interesting about them. Like you won't find the same id in one table in the other table?

Mark
 
Bennett Smithsonian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done that, and it does appear that the same ID cannot appear in the Car table and in the Truck table. That is kind of an accident though -- if I were using an autoincrement for my identifier, there would be the same ID in both tables.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic