[hibernate-issues] [JIRA] (HHH-14040) @LazyToOne(LazyToOneOption.NO_PROXY) relationship fields executing additional queries

James Tran Dung (JIRA) jira at hibernate.atlassian.net
Sun May 24 05:38:24 EDT 2020


James Tran Dung ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5c04bf6c470dea35d6932be9 ) *created* an issue

Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZDdlZWMxYTVkNTc4NGJkYzhmNjBlYWVhZTY0MDlhNjUiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/HHH-14040?atlOrigin=eyJpIjoiZDdlZWMxYTVkNTc4NGJkYzhmNjBlYWVhZTY0MDlhNjUiLCJwIjoiaiJ9 ) HHH-14040 ( https://hibernate.atlassian.net/browse/HHH-14040?atlOrigin=eyJpIjoiZDdlZWMxYTVkNTc4NGJkYzhmNjBlYWVhZTY0MDlhNjUiLCJwIjoiaiJ9 ) @LazyToOne(LazyToOneOption.NO_PROXY) relationship fields executing additional queries ( https://hibernate.atlassian.net/browse/HHH-14040?atlOrigin=eyJpIjoiZDdlZWMxYTVkNTc4NGJkYzhmNjBlYWVhZTY0MDlhNjUiLCJwIjoiaiJ9 )

Issue Type: Bug Affects Versions: 5.4.15 Assignee: Unassigned Created: 24/May/2020 02:38 AM Environment: Windows 10, Spring Boot v2.2.7, Hibernate v5.4.15.Final, `spring-data-jpa-entity-graph` v2.2.8, SQL Server, Java 8 Priority: Critical Reporter: James Tran Dung ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5c04bf6c470dea35d6932be9 )

Version:

* Spring Boot v2.2.7
* Hibernate v5.4.15.Final
* `spring-data-jpa-entity-graph` v2.2.8

I have the following `@Entity` in my application.

@Entity(name= "DemoAccount" )
@Table(name= "staff" )
@Getter @Setter @FieldNameConstants
@NoArgsConstructor
public class Account {
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   protected Long id;
   @Column(name= "username" , nullable= false )
   private String userId;
   private String name;

   @ManyToOne(fetch=FetchType.LAZY)
   @LazyToOne(LazyToOneOption.NO_PROXY)
   @LazyGroup( "nationality" )
   @JoinColumn(name= "nationality" , referencedColumnName= "code" )
   private Nationality nationality;
}

@Entity(name= "DemoNationality " )
@Table(name= "nationality " )
@Getter @Setter @FieldNameConstants
@NoArgsConstructor
public class Nationality {
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   protected Long id;
   @Column(name= "code" , nullable= false )
   private String code;
   private String name;
}

I'm using the following @Repository to look for Account.

@Repository
public interface AccountRepo extends EntityGraphJpaRepository<Account, Long > {
   Optional<Account> findByUserId( String userId, EntityGraph entityGraph);
}

My test endpoint contains a simple method.

@CustomerTransactional
@GetMapping( "/profile" )
public void testProfile(@RequestParam String userId) {
   Optional<Account> account = accountRepo.findByUserId(userId, EntityGraphs.create(Account.Fields.nationality));
   if (account.isPresent()) {
       System.out.println(account.get().getName());
       System.out.println(account.get().getNationality().getName());
   }
}

When I execute this endpoint, this is what I'm seeing in the log.

2020-05-24 16:18:26,492 DEBUG [http-nio-9000-exec-1] org.hibernate.SQL   : 
   /* select
       generatedAlias0 
   from
       DemoAccount as generatedAlias0 
   where
       generatedAlias0.userId=:param0 */ select
           account0_.id as id1_61_0_,
           nationalit1_.id as id1_35_1_,
           account0_.name as name4_61_0_,
           account0_.username as username6_61_0_,
           nationalit1_.code as code2_35_1_,
           nationalit1_.name as name4_35_1_
       from
           staff account0_ 
       left outer join
           nationality nationalit1_ 
               on account0_.nationality=nationalit1_.code 
       where
           account0_.username=?
2020-05-24 16:18:26,492 TRACE [http-nio-9000-exec-1] org.hibernate.type.descriptor.sql.BasicBinder   : binding parameter [1] as [VARCHAR] - [90000010]
Edgar Rey Tann
2020-05-24 16:18:26,506 DEBUG [http-nio-9000-exec-1] org.hibernate.SQL   : 
   /* sequential select
       com.ft.demo.db.customer.domain.Account */ select
           account_.nationality as nationa22_61_ 
       from
           staff account_ 
       where
           account_.id=?
2020-05-24 16:18:26,507 TRACE [http-nio-9000-exec-1] org.hibernate.type.descriptor.sql.BasicBinder   : binding parameter [1] as [BIGINT] - [660]
Indonesian

As you can see, in the middle of the account name and the nationality name, there's an extra query just to select the nationality field. It means if I have 50 records, 50 additional queries got executed. May I know how I can fix this behaviour?

Below is what I used in pom.xml to activate Hibernate Bytecode Enhancer

<build>
   <plugins>
     <plugin>
       <groupId>org.hibernate.orm.tooling</groupId>
       <artifactId>hibernate-enhance-maven-plugin</artifactId>
       <version>${hibernate.version}</version>
       <executions>
         <execution>
           <configuration>
             <failOnError> true </failOnError>
             <enableLazyInitialization> true </enableLazyInitialization>
             <enableDirtyTracking> true </enableDirtyTracking>
             <enableAssociationManagement> true </enableAssociationManagement>
           </configuration>
           <goals>
             <goal>enhance</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
   </plugins>
 </build>

( https://hibernate.atlassian.net/browse/HHH-14040#add-comment?atlOrigin=eyJpIjoiZDdlZWMxYTVkNTc4NGJkYzhmNjBlYWVhZTY0MDlhNjUiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-14040#add-comment?atlOrigin=eyJpIjoiZDdlZWMxYTVkNTc4NGJkYzhmNjBlYWVhZTY0MDlhNjUiLCJwIjoiaiJ9 )

Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100127- sha1:ff24dc8 )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200524/a9ca64d8/attachment.html 


More information about the hibernate-issues mailing list