Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Fetching specified data of abstract class
PostPosted: Fri May 26, 2017 11:16 am 
Newbie

Joined: Fri May 26, 2017 10:59 am
Posts: 2
I have such struture - abstract class A {
String field1;
String field2;
String field3;
},
class B extends A {
//mappingwith fetch type EAGER
List<Child> childs;
String otherField;
}
class C extends A {
String field;
}

and single table strategy with discriminator column mechanism;

My issue is how to fetch all entities of hierarchy - C and B classes but restricted only with A class fields;

When i perform session.createQuery(
"from com.aginity.amp.catalogservice.domain.model.workspace.asset.Asset", A.class).list(); I get all fields of B class with its childs because of EAGER fetch type.

So I tried to workaround it by plain slq query:

session.createNativeQuery("SELECT field1, field2 from A", A.class).list() and got also the same result as in above example.

I would try setResultTransformer but it is deprecated in my hibernate version(5.2.8).

How can I solve this issue?


Top
 Profile  
 
 Post subject: Re: Fetching specified data of abstract class
PostPosted: Fri May 26, 2017 11:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1228
Use a DTO projection with the ResultTransformer. ResultTransformer is deprecated because we want to enhance it, so we are going to replace it with a FuncationInterface.

As for the EAGER associations, you should turn them to LAZY as EAGER is bad for performance.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Fetching specified data of abstract class
PostPosted: Sun May 28, 2017 10:44 am 
Newbie

Joined: Fri May 26, 2017 10:59 am
Posts: 2
vlad wrote:
Use a DTO projection with the ResultTransformer. ResultTransformer is deprecated because we want to enhance it, so we are going to replace it with a FuncationInterface.

As for the EAGER associations, you should turn them to LAZY as EAGER is bad for performance.


Thanks for reply!

You mean Criteria query with projections?
As for EAGER type unfortunally I can't change it to LAZY for business logic reasons.

So if i don't use ResultTransformer because of its deprecation , I must manually move logic of instantiating my classes B and C depends of discriminator field. Is there any other way to instantiate classes with data from projections but not to manually write smth like

if(type == 'B'){
obj = new B();
}

?


Top
 Profile  
 
 Post subject: Re: Fetching specified data of abstract class
PostPosted: Mon May 29, 2017 1:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1228
Quote:
You mean Criteria query with projections?


No. Thats not what I said.

Quote:
As for EAGER type unfortunally I can't change it to LAZY for business logic reasons.


Fetching data on a per business use case is the right thing to do no matter what business requirements have. There is no such thing o having a business use case that tells the data access layer to fetch more data than needed.

Quote:
So if i don't use ResultTransformer because of its deprecation


You can still use it. The deprecation marks that it ill be modified in 6.0. However, many other things will change as well, so the ResultTransformer is probably the least to worry about.

Quote:
I must manually move logic of instantiating my classes B and C depends of discriminator field.


Well, you are supposed to fetch DTOs, not entities for a DTO projection, and choosing the object type is not a tedious process at all.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Fetching specified data of abstract class
PostPosted: Mon May 29, 2017 2:57 am 
Newbie

Joined: Sat May 21, 2011 7:40 am
Posts: 19
Blaze-Persistence Entity Views might just be what you are looking for.
You annotate the mappings between your DTOs and your entity model and then simply fetch the DTO objects while getting an optimal query for the desired projection.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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.