-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: @NamedQuery + @NamedEntityGraph fetch doesn't work
PostPosted: Tue Sep 12, 2017 1:11 am 
Newbie

Joined: Tue Sep 12, 2017 12:48 am
Posts: 2
Hello, I'm Java beginner. I'm trying to use JPA with Hibernate and I've got an issue that I don't understand. It seems to me I found a bug, but maybe I'm doing something wrong.
For example I have two entities with OneToOne relation and EntityGraph to minimize queries count.

Code:
@Entity
@Data
public class Department {

    @Id
    @GeneratedValue
    private long id;

    private String name;

    @OneToMany(mappedBy = "department")
    private List<Person> persons;

    @OneToOne
    private DepartmentType type;

    @Override
    public String toString() {
        return "Department id=" + this.id + " " + this.name;
    }

}


@Entity
@NamedQuery(name = "departmetTypes",
            query = "select dt from DepartmentType dt",
            hints = {@QueryHint(name=QueryHints.HINT_LOADGRAPH, value = "DepartmentType.withDepartments")}
)
@NamedEntityGraph(name="DepartmentType.withDepartments", attributeNodes={@NamedAttributeNode("department")})
@Data
public class DepartmentType {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @OneToOne(mappedBy="type")
    private Department department;
}


The following code gives only one query (as I wish)
Code:
public static void main(String[] args) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        EntityGraph gr = em.getEntityGraph("DepartmentType.withDepartments");
        em.createQuery("from DepartmentType", DepartmentType.class).setHint(QueryHints.HINT_LOADGRAPH, gr).getResultList().forEach(
                dt-> System.out.println(dt.getDepartment())
        );
        em.getTransaction().commit();

    }


Named query gives N+1 queries

Code:
    public static void main(String[] args) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.createNamedQuery("departmetTypes", DepartmentType.class).getResultList().forEach(
                p-> System.out.println(p.getDepartment()));
        em.getTransaction().commit();

    }


But named query with manually setted hint works correctly
Code:
public static void main(String[] args) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.createNamedQuery("departmetTypes", DepartmentType.class)
                .setHint(QueryHints.HINT_LOADGRAPH, em.getEntityGraph("DepartmentType.withDepartments"))
                .getResultList().forEach(
                p-> System.out.println(p.getDepartment()));
        em.getTransaction().commit();

    }

bun it looks like overhead. Can I use NamedQueries with EntiTyGraph hint?

Sorry for my English.


Top
 Profile  
 
 Post subject: Re: @NamedQuery + @NamedEntityGraph fetch doesn't work
PostPosted: Tue Sep 12, 2017 3:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It looks like a bug to me too. You should create a replicating test case using this template and open a Jira issue.

Thanks


Top
 Profile  
 
 Post subject: Re: @NamedQuery + @NamedEntityGraph fetch doesn't work
PostPosted: Tue Sep 12, 2017 5:16 am 
Newbie

Joined: Tue Sep 12, 2017 12:48 am
Posts: 2
I found that org.hibernate.annotations.NamedQuery doesn't contains "hints" definition while JPA does.

Code:
@Target( { TYPE, PACKAGE })
@Retention(RUNTIME)
@Repeatable(NamedQueries.class)
public @interface NamedQuery {

   /**
    * The name of this {@code NamedQuery}.
    */
   String name();

   /**
    * The query string for this {@code NamedQuery}.
    */
   String query();

   /**
    * The flush mode for this query.
    */
   FlushModeType flushMode() default FlushModeType.PERSISTENCE_CONTEXT;

   /**
    * Whether the query (results) is cacheable or not.  Default is {@code false}, that is not cacheable.
    */
   boolean cacheable() default false;

   /**
    * If the query results are cacheable, name the query cache region to use.
    */
   String cacheRegion() default "";

   /**
    * The number of rows fetched by the JDBC Driver per trip.
    */
   int fetchSize() default -1;

   /**
    * The query timeout (in seconds).  Default is no timeout.
    */
   int timeout() default -1;

   /**
    * A comment added to the generated SQL query.  Useful when engaging with DBA.
    */
   String comment() default "";

   /**
    * The cache mode used for this query.  This refers to entities/collections returned from the query.
    */
   CacheModeType cacheMode() default CacheModeType.NORMAL;

   /**
    * Whether the results should be read-only.  Default is {@code false}.
    */
   boolean readOnly() default false;
}


Code:
@Target({TYPE})
@Retention(RUNTIME)
public @interface NamedQuery {

    /**
     * (Required) The name used to refer to the query with the {@link EntityManager}
     * methods that create query objects.
     */
    String name();

    /** (Required)
     * The query string in the Java Persistence query language.
     */
    String query();

    /**
     * (Optional) The lock mode type to use in query execution.  If a <code>lockMode</code>
     * other than <code>LockModeType.NONE</code> is specified, the query must be executed in
     * a transaction.
     * @since Java Persistence 2.0
     */
    LockModeType lockMode() default NONE;

    /** (Optional) Query properties and hints.  May include
     * vendor-specific query hints.
     */
    QueryHint[] hints() default {};
}


Top
 Profile  
 
 Post subject: Re: @NamedQuery + @NamedEntityGraph fetch doesn't work
PostPosted: Tue Sep 12, 2017 5:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Add that to the issue as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.