3

I'm having this situation in Hibernate where there is a parent-child relationship between two classes, but there is no such relationship in the database, since the child class represents a view, not a table.

Now, the problem is that for Hibernate to be happy the Inheritance annotation needs to be provided, otherwise Hibernate will consider some default value and try to look for a non-existent "dtype" column.

I have tried to use TABLE_PER_CLASS as the strategy (the default is SINGLE_TABLE), but in this case Hibernate does some kind of union with the view associated with the child class, which doesn't make sense here, and some error is raised about a column that doesn't exist -- a column that exists in both the table and the view, but any way.

The question would be how to properly model this in terms of annotations or, alternatively, how to ask for the parent entity without producing an underlying query that involves the child. (The second question was asked here. I didn't give that a try yet because I'm hoping for a better solution.)

Edit: to better illustrate the explanation, here is a piece of code equivalent to my setup:

@Entity
@Table(name = "PARENT")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class ParentEntity implements Serializable {
    // ...
}

@Entity
@Table(name = "PARENT_VIEW")
public class ChildEntity extends ParentEntity implements Serializable {
    // ...
}
Community
  • 1
  • 1
DanielM
  • 1,106
  • 3
  • 17
  • 27

1 Answers1

-1

I can't understand the modelling. Why was it done like this?

If the 'child' is a view on the 'parent' table then it is not really a child and should not be modeled like one.

Even if the view is modifiable (as in, inserts get inserted into the 'parent' table etc.) this is basically just doing yourself what hibernate will do for you (as in, a query on 'child' will effectively produce a subset of 'parent' elements).

What are you trying to achieve with this view structure and why was it decided you need to model it this way?

It seems to me the solution is to either not model this as a parent-child relationship or to use an inheritancetype of single_table and remove the table annotation from the child (using a normal parent-child relationship in hibernate).

Buurman
  • 1,914
  • 17
  • 26