Question

I have looked on the forum for similar questions but haven't found anything that addresses what I'm looking for (at least what i can tell).

I have a situation where we have multiple clients with multiple request types each. What I was trying to accomplish was having a Parent table "Request" that splits into a child table for each client - "ClientARequest", "ClientBRequest". From there, I would have multiple classes that map to the same client table. "ClientARequest1" and "ClientARequest2" both map to "ClientARequest", and similar for ClientB. I can get the first part working no problem with JOINED inheritance. I can get the second working as well for saving. However, when fetching, hibernate is creating an instance of the last mapped class and so depending on how I am using the fetched object, I'll get an ClassCastException or I won't have the correct data because it is the wrong Class.

@Entity(name="Request")
@Table(name = "REQUEST")
@Inheritance(strategy=InheritanceType.JOINED)  
@DiscriminatorColumn(name="MY_TYPE", discriminatorType=DiscriminatorType.STRING)
public class Request  { ... }

@Entity(name="ClientARequest1")
@Table(name = "CLIENTAREQUEST")
@DiscriminatorValue("ClientA")
public class ClientARequest1 extends Request { ... }

@Entity(name="ClientARequest2")
@Table(name = "CLIENTAREQUEST")
@DiscriminatorValue("ClientA")
public class ClientARequest2 extends Request { ... }

Same thing for ClientB:

@Entity(name="ClientBRequest1")
@Table(name = "CLIENTBREQUEST")
@DiscriminatorValue("ClientB")
public class ClientBRequest1 extends Request { ... }

@Entity(name="ClientBRequest2")
@Table(name = "CLIENTBREQUEST")
@DiscriminatorValue("ClientB")
public class ClientBRequest2 extends Request { ... }

So, like I mentioned, the three tables, Request, ClientARequest, and ClientBRequest are created and this is no problem discrimating between tables. It is when trying to fetch from a table that hibernate uses the 2nd mappings to create an instance of a class.

Ex.

ClientARequest1 r = new ClientARequest1("ClientA");
dao.save(r);

Works as expected.
However for:

Request r = (Request) dao.findById(1l);  //where id==1 is a ClientARequest1

will actually pull back a ClientARequest2.

Any thoughts would be much appreciated. Is this solution is not doable, is there a way to break out the tables down another level?

2/26/13 11:19 AM EST:
I've attempted the solution in this thread How to mix inheritance strategies with JPA annotations and Hibernate? , but I couldn't map the subclasses BB1, BB2, CC1, CC2 properly, as their properties would be placed in the base-parent table A. I would want their properties placed in their direct parent tables BB, CC respectively.

Was it helpful?

Solution

I got it. I followed the solution found in How to mix inheritance strategies with JPA annotations and Hibernate?. The only things I changed was adding @MappedSuperclass to the BB, CC level, and moving the @Entity and @SecondaryTable to the BB1, BB2, CC1, CC2 level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top