Solved

Using JPA to partially initialize entities

Posted on 2011-02-17
7
1,555 Views
Last Modified: 2013-11-24
Hi,

I have created an application with a relatively complex model. Everything was working fine and the beta-tests were running smoothly. The problem I am having is that I am using BlazeDS to send results to a Flex client. This causes all relations to be loaded (even if they are marked for lazy loading) and sent to the client. This is rather undesirable as a simple listEntityA doesn't only return a list of EntityA but also all of their references and the references of their references ... a lot of stuff.

I know in JPA I can do these really cool "SELECT NEW packagename.Classname(params...) FROM ..." to create unmanaged instances that are partially initialized (projections). The problem is, that I really would like the main entity to be fully initialized, but I would love some related objects to be only initialized with let's say "id" and "name" (all other properties left empty) ... this would be a projection of the relation.

How could I do this?
0
Comment
Question by:ChristoferDutz
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34915055
Would not claim that I understand much of what you are writing about, but this is the link with quite similar words,
who knows, maybe you haven't seen it:

http://ria.dzone.com/articles/lazy-loading-flex-blazeds-hibernate
0
 
LVL 20

Author Comment

by:ChristoferDutz
ID: 34915254
I just added the Flex, BlazeDS, LazyCollections words in order to set the focus. My system works great without any errors. I am currently optimizing. Let me explain my problem in a little more detail

I have the following entities with the properties:

EntityA
- id
- name
- oneToMany to EntityB
- oneToMany to EntityC

EntityB
- id
- name
- oneToOne to EntityD

EntityC
- id
- name
- (big blob of data)

EntityD
- id
- name
- (big blob of data)

Open in new window


Now immagine I want to load a list of EntityA objects. When displaying one of the EntityA Objects in a list, I want to output the name of all EntityBs that are associated with that entity and output a link behind the name to switch to that entities view (I only need the id and name and not all the other properties).

So I am looking for a way to do the projection select on the related objects and not the main entity.
0
 
LVL 1

Expert Comment

by:zabin7
ID: 34925099
What I understood is u want to load only the parent record but not the child record. If that is the case then you can use lazy loading

@OneToMany(mappedBy = "type", fetch = FetchType.LAZY)
    private List<Type> type= new ArrayList<Type>();
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 20

Author Comment

by:ChristoferDutz
ID: 34925118
No ... I want to load the parent and I want to partially load the children.
I can load an entity partially by using projection in my select-statement "SELECT NEW de.cware.cweb.model.User(usr.id, usr.name) FROM User usr"
What I would like to do, ist to load the parrent fully and the related entities partially in a way that in the related objects only the id and name properties are initialized.


0
 
LVL 5

Accepted Solution

by:
aciuica earned 500 total points
ID: 34928956
Maybe it depends of which JPA implementation you use (if supports fields lazy loading), but annotating some fields from related entities with lazy loading can be a soution.
http://www.java2s.com/Tutorial/Java/0355__JPA/MarkFieldAsLazyLoad.htm
0
 
LVL 20

Author Comment

by:ChristoferDutz
ID: 34954489
I'm currently stuck with an ANTLR problem ... I'l check this lazy loaded property thing you mentioned ... sounds very prommissing :-)
0
 
LVL 20

Author Closing Comment

by:ChristoferDutz
ID: 34996366
Thanks for the tip with the lazy-loaded properties.

I actually solved my problem by going from JPA to Jdbc directly as here I could controll exactly what I wanted to do.

Thanks anyway.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 69
How to remove numeric and alpha from an alphanumeric field? 5 71
syadmin MSSQL 2 58
Unrecognized Database Format 8 91
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now