Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Weaving question

Hi all,

Try putting the fields you want to load lazily in a secondary table. You need to specify a secondary table at the class level and then specify that the field is to be stored in the secondary table and loaded lazily. This worked for me (using JPA1, Glassfish 2.1).

See here for good snippet:
http://www.java2s.com/Tutorial/Java/0355__JPA/MapOneFieldToSecondaryTable.htm

Michael Bar-Sinai


On 4 Mar, 2010, at 4:02 PM, Peter Krogh wrote:

> Michael,
> 
> In JPA by default 1:1s and M:1 s are Eagerly fetched.  Are you assuming otherwise, or explicitly setting to Lazy?
> 
> Peter
> 
> -----Original Message-----
> From: Michael Simons [mailto:michael.simons@xxxxxxxxxxx]
> Sent: Thursday, March 04, 2010 7:17 AM
> To: EclipseLink User Discussions
> Subject: Re: [eclipselink-users] Weaving question
> 
> 
> Hello Rafal,
> 
> I've got another case where lazy loading is not done like expected:
> The part of my model:
> @Entity class Scenario {
>  LoginUser owner; //<*:1> LoginUser
> }
> @Entity class LoginUser {
>  Language language; //<*:1> Language
> }
> @Entity class Language{...}
> The model is statically woven.
> 
> The code is
>  /*1*/ Query q = em.createQuery ("select s from Scenario s where s.id = 1");
>  /*2*/ Scenario scenario = (Scenario)q.getSingleResult ();
>  /*3*/ System.out.printf ("%s\n", scenario.getOwner ());
> 
> In line 2 it works like expected, the generated SQL is
> SELECT scenario_id, creation_date, ..., login_user_id FROM scenario WHERE (scenario_id = ?);
> 
> However in line 3 the following two queries are run:
> SELECT login_user_id, ..., lnguage_id FROM login_user WHERE (login_user_id = ?);
> SELECT lnguage_id, nme, abbreviation FROM lnguage WHERE (lnguage_id = ?);
> 
> So it seems like lazy-loading is just done in the first run, but forgotten when the lazy-load is
> done.
> 
> That's difficult to handle for me, because I don't know how EL determines when/what it does.
> 
> Kind Regards, Michael
> 
> Swierzynski, Rafal schrieb:
>> Hi EL users and devs,
>> 
>> after seeing some email on weaving in SE, I decided to try this myself. I configured it and it works. However, I noticed something strange.
>> My @Entity class is very simple: Id (Long), Name (String, eager), Huge (String, lazy). Now when I invoke EntityManager.find() the first SQL is:
>> SELECT ID, NAME FROM PERSON WHERE (ID = ?)
>> which proves that Huge is lazy loaded. Now, I get this via a getter, and the SQL generated is this:
>> SELECT ID, HUGE, NAME FROM PERSON WHERE (ID = ?)
>> 
>> As you can see, the same data (Id and Name in this case) is selected twice from the database. In my case it is very little data, but I can imagine a scenario when a lot of data gets selected twice. Maybe I am missing some configuration property?
>> 
>> Then, I added another LAZY field, huge2. They don't get selected at first, as planned, but when I reference any of the lazy fields, both are selected.
>> 
>> Are these 2 use cases supposed to work this way, or should I configure something additionally?
>> 
>> As a side question: to satisfy my curiosity, I output and decompiled the weaved class, and the source is much different. However, I could debug through the entity, and it worked fine, the breakpoints were put in the appropriate lines and the debugger worked perfectly, it was totally invisible that the class's bytecode was changed and didn't correspond to the (original) source. How did you do this? What magic did you use for that? Do you mess with the internal class file's sumbol tables somehow to map the new bytecode to the old source or something?
>> 
>> Regards,
>> RafaƂ
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> 
> 
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top