Naga
Naga

Reputation: 11594

How to use subgraph in spring-data(with hibernate)?

I'm using spring boot 1.2.5 and Hibernate 4.3.10 via spring data, and I wrote the code below.
I want to use subgraph so that I can retrieve data when I get the entity graph named "content.search". Here is my code.

@NamedEntityGraphs({@NamedEntityGraph(name = "content.search",
 includeAllAttributes=true,
attributeNodes = {
    @NamedAttributeNode("mstItem")
   ,@NamedAttributeNode(value="itemTypeGraph",subgraph="itemTypeGraph") 
    }, subgraphs = {@NamedSubgraph(name = "itemTypeGraph",
    attributeNodes = {@NamedAttributeNode("mstItemType")
    ,@NamedAttributeNode("mstItemName")
      }
    )
  }
)}) 
public class Content implements java.io.Serializable { ... }

When I specify this line, it produce error as below.(without this line, it works fine but can not retrieve data in subgraph as EAGER type (can retrieve later as LAZY)

,@NamedAttributeNode(value="itemTypeGraph",subgraph="itemTypeGraph")

Here is the error which I encountered.

[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [itemTypeGraph] on this ManagedType [com.example.domain.Content]

from the other question, this error seems fixed before hibernate 4.3.9 or later, but I still encounter this error and can not get data in subgraph... why it can not find itemTypeGraph? and how should I change the code?

Upvotes: 0

Views: 1841

Answers (1)

Koraktor
Koraktor

Reputation: 42903

The subgraph has to be defined on the attribute node it should be applied to.

So the correct definition should look like this:

@NamedEntityGraph(name = "content.search", includeAllAttributes=true,
    attributeNodes = {
        @NamedAttributeNode("mstItem", subgraph = "itemTypeGraph") 
    }, subgraphs = {
        @NamedSubgraph(name = "itemTypeGraph", attributeNodes = {
            @NamedAttributeNode("mstItemType"), @NamedAttributeNode("mstItemName")
        })
    })
public class Content implements java.io.Serializable { ... }

Upvotes: 1

Related Questions