2009-11-09 65 views
7

我正在開發一個應用程序,其中所有的pojos都作爲接口公開,但我們映射了真正的實現class.we使用spring和JPA annotation.i'm來測試一對一的關係,並且我在界面上遇到了一個小問題。Hibernate與interface.i的一對一映射需要建議

造成的:org.springframework.beans.factory.BeanCreationException:錯誤創建名稱爲豆 'sessionContainer' 在類路徑資源定義[META-INF /模型-config.xml中]:
無法解析參考當設置構造函數參數時,bean'sessionFactory';嵌套異常是org.springframework.beans.factory.BeanCreationException:
在類路徑資源[META-INF/model-config.xml]中定義名稱爲'sessionFactory'的Bean時出錯:
調用init方法失敗;嵌套異常是org.hibernate.AnnotationException:
com.mycompany.project.subproject.model.UserAccountImpl.profile @OneToOne或@ManyToOne引用未知實體:com.mycompany.project。

所以這個類之前的所有其他類映射是否按預期工作,所以我將只發布applicationContext文件我命名爲model-config.xml

<property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
     </props> 
    </property> 
    <property name="annotatedClasses"> 
     <list> 
      ... 
      <value>com.mycompany.project.subproject.model.UserProfileImpl</value> 
      <value>com.mycompany.project.subproject.model.UserAccountImpl</value> 
      ... 
     </list> 
    </property> 

這裏的一部分是兩個所涉及的類UserProfileImpl.javaUserAccountImpl.java

//UserAccountImpl Class 
@Entity 
@Table(name ="USER_ACCOUNT") 
public class UserAccountImpl implements UserAccount { 

    @Id @GeneratedValue 
    @Column(name="USER_ACCOUNT_ID") 
    private Long ID; 

    ... 

    @OneToOne 
    @JoinColumn(name="USER_PROFILE_ID") 
    private UserProfile profile; 

    ... 
} 

//UserProfileImpl class 
@Entity 
@Table(name="USER_PROFILE") 
public class UserProfileImpl implements UserProfile { 

@Id @GeneratedValue 
@Column(name="USER_PROFILE_ID") 
private Long ID; 
.... 

@OneToOne(mappedBy="profile") 
private UserAccount userAccount; 
.... 

}

我還是不是很舒適與休眠尚未所以我不知道我是否應該在UserAccountImpl更改UserProfile參考UserProfileImpl。然後又同樣可以在UserProfileImpl發生了userAccount參考,因爲它是一個雙向導航的東西。 什麼是不會破壞結構一致性的最佳選擇? 感謝您閱讀本

+0

你能否闡述一下爲什麼你正在使用POJO的接口? – dlinsin 2009-11-09 13:03:33

+0

Hibernate不可能知道你需要哪個類來實現'UserAccount',因此你必須使用像'@david'這樣的'targetEntity',因爲Hibernate不能推斷出這個。 – 2009-11-10 05:46:13

回答

2

你可以嘗試以下方法:

@OneToOne(mappedBy="profile", targetEntity=UserAccountImpl.class) 
private UserAccount userAccount 
+0

謝謝布魯夫這是最簡單的one.will嘗試它 – 2009-11-09 13:53:38

3

您有以下選擇:

  1. 你必須讓Hibernate瞭如何充分利用該接口UserAccount的類。目前,最簡單的解決方案是使用具體類型而不是您的UserProfileImpl中的接口。

  2. 您可以使用@Target來指定要使用的實現(請參閱[文檔] [1])。

  3. 您可以將該字段與自定義UserType進行映射。這允許在運行時選擇映射(用於接口的實現),但是您必須編寫代碼以便自己在業務對象和數據庫之間複製字段(不再自動映射)。

+0

感謝您的洞察力。將回到you.thanks很多 – 2009-11-09 13:54:11

+0

這一個爲我工作再次感謝 – 2009-11-09 16:06:29

0

確實用戶配置必須是一個獨立的實體?您可以將其設置爲組件,並將UserAccount和UserProfile表組合成一個。你的對象模型仍然有一個單獨的UserProfile對象,它只是一個UserAccount擁有的值對象。

並不是每一個對象必須被實現爲一個實體,而一到一個映射在實踐中相當罕見....

+0

mmmh聽起來很適合here.But我不是很好(我的意思是我會必須閱讀)與組件。所以我會在下一個里程碑看看它。但絕對你的解決方案對我來說很酷。謝謝 – 2009-11-09 13:56:34