2015-01-14 42 views
2

我嘗試了許多不同的事情並進行了大量的研究,但仍未能得到以下工作:我想在實體上定義一個@NamedEntityGraph,但我指的屬性位於父類,其定義爲@MappedSuperclass在@MappedSuperclass上使用@NamedEntityGraph屬性

我有一種輕微的感覺(同樣在檢查代碼後,實體圖是如何構建的),這是行不通的,但也許有人可以對此有所瞭解。

下面是詳細說一下,我們正在努力,以(一些事情縮短):

@MappedSuperclass 
public class UserBase { 
    private Long id; 
    private String loginname; 
    private String password; 

    @ManyToMany(fetch = FetchType.LAZY) 
    private Set<Role> roles; 
    .... 
} 

和實際的實體:

@Entity 
@NamedEntityGraph(
    name = "withRoles", 
    attributeNodes = { @NamedAttributeNode("roles") }) 
public class User extends UserBase { 
    .... 
} 

發生的是,我們得到了錯誤

Unable to locate Attribute with the the given name [roles] on this ManagedType [some.package.path.User] 

單步執行圖創建代碼我可以看到它只查看了在類i上定義的屬性自己,沒有超類型。任何機會我都可以用這種方式定義圖表,這是否有效?

感謝您的任何提示!

+0

你的getter getId()是用什麼標註的?你能發佈那部分代碼嗎? –

回答

0

嘗試增加includeAllAttributes=true

@NamedEntityGraph(
    name = "withRoles", 
    includeAllAttributes=true 
    attributeNodes = { @NamedAttributeNode("roles") }) 
+0

不幸的是沒有幫助。還是一樣的例外: - / –

0

這似乎是entityGraphs應該在基類中定義,並添加他們那裏所有的屬性和使用subclassSubgraphs

http://docs.oracle.com/javaee/7/api/javax/persistence/NamedEntityGraph.html

檢索

但是......我期望它通過從註釋類的實際子類中獲取名稱而以不同的方式工作。這樣我就可以在我的子類中添加節點。

它看起來像是以另一種方式工作,所以你可以讓你的基類註釋依賴於子類的細節。

順便說一句! otehr男人的答案似乎是正確的,你只需要刪除attributeNodes = { @NamedAttributeNode("roles") })。而父母的屬性將在那裏。