2013-07-10 50 views
0

我們有一個用戶與Preference有一對一的關係(該屬性名爲storedPreference)。假設一個用戶將CustomPreference的一個實例(Preference的一個子類)保存爲它的storedPreference。獲取storedPreference並將其轉換爲CustomPreference會導致ClassCastException,但爲什麼?如何保存併爲休眠屬性加載子類的實例

(CustomPreference)theUser.getStoredPreference() 

導致

java.lang.ClassCastException: core.preferences.Preferences_$$_javassist_82 cannot be cast to hb.core.preferences.CustomPreferences 
    at hb.base.user.user.HadithUser.getPreferences(HadithUser.java:39) 
    at hb.base.user.user.HadithUser.getPreferences(HadithUser.java:23) 
    at core.form.base.ThemeForm.getTheme(ThemeForm.java:116) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:87) 
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67) 
    at org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:179) 
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183) 
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) 
    at org.primefaces.renderkit.HeadRenderer.encodeBegin(HeadRenderer.java:85) 
    at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:587) 
    at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:517) 
    at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541) 
    at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1981) 
    at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:298) 
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:59) 
    at org.apache.myfaces.tomahawk.application.ResourceViewHandlerWrapper.renderView(ResourceViewHandlerWrapper.java:93) 
    at org.springframework.faces.webflow.FlowViewHandler.renderView(FlowViewHandler.java:99) 
    at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116) 
    at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:199) 
    ... 84 more 

這裏有實體的定義:

@Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
public class Preferences { 
    ... 
} 


@Entity 
public class CustomPreferences extends Preferences { 
    ... 
} 


@Entity 
public class User { 
    @OneToOne(fetch = FetchType.LAZY) 
    public Preferences getStoredPreferences() { 
     return storedPreferences; 
    } 

    ... 
} 

我檢查了數據庫,以確保此實例DTYPE值CustomPreference,這是正確的

回答

0

這與JPA無關。這是java計劃,超類引用可以引用子類對象,但超類對象不能被子類引用。

(CustomPreference)theUser.getStoredPreference(); 

偏好對象可通過偏好型或它的超類型被稱爲而不是由它的亞型即CustomPreference.And如果試圖這樣做,它給ClassCasteException。

+0

保存爲storedPreference的對象是通過超類引用引用的CustomPreference。所以應該沒有問題將其轉換回子類。 – rabolfazl