Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] The usage of the EntityGraph

Hi all,

I am trying to use EntityGraph, but it seems it does not worked as expected.

The code is hosted on my github.com.

https://github.com/hantsy/ee7-sandbox/tree/master/jpa/entitygraph

I deifined te EntityGraph in the Post class like this.

@NamedEntityGraph(
        name = "post",
        attributeNodes = {
            @NamedAttributeNode("title"),
            @NamedAttributeNode(value = "comments", subgraph< /span> = "comments")
        },
        subgraphs = {
            @NamedSubgraph(
                    name = "comments",
                    attributeNodes = {
                        @NamedAttributeNode("content")}
            )
        }
)
public class Post implements Serializable {} And used it in the ViewPostBean class.
 EntityGraph postEntityGraph=em.getEntityGraph("post");
// EntityGraph postEntityGraph=em.createEntityGraph(Post.class);
// postEntityGraph.addAttributeNodes("title");
// postEntityGraph.addSubgraph("comments").addAttributeNodes("content");
            
            this.post=em
                    .createQuery("select p from Post p where p.id=:id", Post.class)
                    .setHint("javax.persistence.loadgraph", postEntityGraph)
                    .setParameter("id", this.id)
                    .getResultList()
                    .get(0);
            
            PersistenceUnitUtil util=em.getEntityManagerFactory().getPersistenceUnitUtil();
            
            log.info("title is loadded@"+util.isLoaded(this.post, "title"));
           &nbs p;log.info("body is loadded@"+util.isLoaded(this.post, "body"));
            log.info("comments is loadded@"+util.isLoaded(this.post, "comments"));

When I saw the logging info, it did not work as expected.

Any help is appreciated.

BTW, how EclipseLink process the lazy association?

In Hibernate, if I set an OneToMany(Lazy) association between Post and Comment, I will get a classic LazyInitializedException in the faceslet view page.

But I can not produces it in EclipseLink....


Thanks.

Hantsy




This email is free from viruses and malware because avast! Antivirus protection is active.



Back to the top