开发者

Hibernate: from inheritance to composition

I have a domain model that is currently heavily relying on inheritance, and due to several issues related to Hibernate's proxy model I am refactoring the model to use composition instead. The main issue that I've been running into is the following situation:

  1. Get record X as Parent
  2. Hibernate returns proxy to Parent
  3. instance of and downcasting are now broken

In addition, thi开发者_JS百科ngs like foreign key references back to the parent table won't work with the table per concrete subclass strategy. So now I'm basically making Parent a concrete class and putting a reference to Child1 and Child2 into parent (requiring a join to fetch the data as needed).

Based on this description, has anyone been down a similar path and encountered any issues, or does this approach make sense? Is there a better approach I haven't thought of?


I've never had to convert from one to the other, but I've experienced both types, and you're headed in the right direction. The problem is actually that instanceof and downcasting used in that way break polymorphism, which Hibernate assumes you're using instead of subverting. Moving toward a true object-oriented model is a step in the right direction.


If i understood you right, in your example parent is the superclass and child1 and child2 are the subclasses. Most of the time, composition is the better choice, so because i do not know any details of your model i would say your on the right way. However i think it would be better if your child1 and child2 would have a reference to parent, because they "extend" parent. The parent should not even know it is extended, so the parent should not have a reference to the childs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜