You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by jlawmi <Ja...@gmail.com> on 2009/03/20 22:42:58 UTC

hibernate conversion items

In unit tests (technically integration tests) where I test my mappings,  I
used to call (in hibernate)
Hibernate.isInitialized(someentity.property) to validate things were lazy or
to generally check their load status. Also, Hibernate had
Hibernate.initialize(sometntity.property) to programmatically force
initialization. Is there an easy way to do this in openjpa.? I see the
fetchgroups (which are nice), but configuring a fetchgroup seems overkill in
some cases...

Can someone point me to equivalent items in openjpa?

Thanks!
James
-- 
View this message in context: http://n2.nabble.com/hibernate-conversion-items-tp2511309p2511309.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: hibernate conversion items

Posted by Pinaki Poddar <pp...@apache.org>.
> however this should be a nice to have API in OpenJPA itself.

Yes. JPA 2.0 has defined more elaborate API for similar purpose in
avax.persistence.spi.PersistenceProvider. OpenJPA will implement these
features as part of  JPA 2.0 contract.



Nitish Kumar-2 wrote:
> 
> Thanks Pinaki,
>           I will try to update my code as per your suggestion, however
> this
> should be a nice to have API in OpenJPA itself.
> 
> Thanks and Regards,
> Nitish Kumar
> 
> On Mon, Mar 23, 2009 at 7:25 PM, Pinaki Poddar <pp...@apache.org> wrote:
> 
>>
>> Hi,
>>  Following suggestion as per Nitish will work with few caveats.
>> 1. It will not work for detached instances. Because detached instances
>> can
>> not access ClassMetaData.
>> 2. The loop to search through fields can go wrong (because indexing is
>> not
>> one-to-one in all cases) and not necessary if ClassMetaData is available.
>> The correct way is
>>   ClassMetaData metaData = sm.getMetaData();
>>   FieldMetaData fmd = metaData.getField(property);
>>   return sm.getLoaded().get(fmd.getIndex());
>> 3. For detached instance, to get ClassMetaData, one will need somehow to
>> access
>>
>> OpenJPAConfiguration.getMetaDataRepositoryInstance().getCachedMetaData(pc.getClass());
>>  then the rest will follow.
>>
>> public static boolean isPropertyLoaded(Object entity, String property) {
>>                PersistenceCapable pc = (PersistenceCapable) entity;
>>                StateManagerImpl sm = (StateManagerImpl)
>> pc.pcGetStateManager();
>>                ClassMetaData metaData = sm.getMetaData();
>>                FieldMetaData[] fields = metaData.getFields();
>>                int dx = 0;
>>                for (int j = 0; j < fields.length; j++) {
>>                        if (fields[j].getName().equals(property)) {
>>                                dx = j;
>>                                break;
>>                        }
>>                }
>>                return sm.getLoaded().get(dx);
>>        }
>>
>> jlawmi wrote:
>> >
>> >
>> > Thanks Nitish. In hibernate calling a getter doesn't force
>> initialization,
>> > at least on many-one fields, but I'll try it out. Since openjpa doesn't
>> > use cglib that makes sense.
>> >
>> > --James
>> >
>>
>>
>> -----
>> Pinaki Poddar                      http://ppoddar.blogspot.com/
>>
>> http://www.linkedin.com/in/pinakipoddar
>> OpenJPA PMC Member/Committer
>> JPA Expert Group Member
>> --
>> View this message in context:
>> http://n2.nabble.com/hibernate-conversion-items-tp2511309p2521187.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
>>
> 
> 


-----
Pinaki Poddar                      http://ppoddar.blogspot.com/
                                      
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
-- 
View this message in context: http://n2.nabble.com/hibernate-conversion-items-tp2511309p2521552.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: hibernate conversion items

Posted by Nitish Kumar <ni...@gmail.com>.
Thanks Pinaki,
          I will try to update my code as per your suggestion, however this
should be a nice to have API in OpenJPA itself.

Thanks and Regards,
Nitish Kumar

On Mon, Mar 23, 2009 at 7:25 PM, Pinaki Poddar <pp...@apache.org> wrote:

>
> Hi,
>  Following suggestion as per Nitish will work with few caveats.
> 1. It will not work for detached instances. Because detached instances can
> not access ClassMetaData.
> 2. The loop to search through fields can go wrong (because indexing is not
> one-to-one in all cases) and not necessary if ClassMetaData is available.
> The correct way is
>   ClassMetaData metaData = sm.getMetaData();
>   FieldMetaData fmd = metaData.getField(property);
>   return sm.getLoaded().get(fmd.getIndex());
> 3. For detached instance, to get ClassMetaData, one will need somehow to
> access
>
> OpenJPAConfiguration.getMetaDataRepositoryInstance().getCachedMetaData(pc.getClass());
>  then the rest will follow.
>
> public static boolean isPropertyLoaded(Object entity, String property) {
>                PersistenceCapable pc = (PersistenceCapable) entity;
>                StateManagerImpl sm = (StateManagerImpl)
> pc.pcGetStateManager();
>                ClassMetaData metaData = sm.getMetaData();
>                FieldMetaData[] fields = metaData.getFields();
>                int dx = 0;
>                for (int j = 0; j < fields.length; j++) {
>                        if (fields[j].getName().equals(property)) {
>                                dx = j;
>                                break;
>                        }
>                }
>                return sm.getLoaded().get(dx);
>        }
>
> jlawmi wrote:
> >
> >
> > Thanks Nitish. In hibernate calling a getter doesn't force
> initialization,
> > at least on many-one fields, but I'll try it out. Since openjpa doesn't
> > use cglib that makes sense.
> >
> > --James
> >
>
>
> -----
> Pinaki Poddar                      http://ppoddar.blogspot.com/
>
> http://www.linkedin.com/in/pinakipoddar
> OpenJPA PMC Member/Committer
> JPA Expert Group Member
> --
> View this message in context:
> http://n2.nabble.com/hibernate-conversion-items-tp2511309p2521187.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

RE: hibernate conversion items

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  Following suggestion as per Nitish will work with few caveats.
1. It will not work for detached instances. Because detached instances can
not access ClassMetaData.
2. The loop to search through fields can go wrong (because indexing is not
one-to-one in all cases) and not necessary if ClassMetaData is available.
The correct way is
   ClassMetaData metaData = sm.getMetaData();
   FieldMetaData fmd = metaData.getField(property);
   return sm.getLoaded().get(fmd.getIndex());
3. For detached instance, to get ClassMetaData, one will need somehow to
access
OpenJPAConfiguration.getMetaDataRepositoryInstance().getCachedMetaData(pc.getClass());
  then the rest will follow. 

public static boolean isPropertyLoaded(Object entity, String property) {
                PersistenceCapable pc = (PersistenceCapable) entity;
                StateManagerImpl sm = (StateManagerImpl)
pc.pcGetStateManager();
                ClassMetaData metaData = sm.getMetaData();
                FieldMetaData[] fields = metaData.getFields();
                int dx = 0;
                for (int j = 0; j < fields.length; j++) {
                        if (fields[j].getName().equals(property)) {
                                dx = j;
                                break;
                        }
                }
                return sm.getLoaded().get(dx);
        } 

jlawmi wrote:
> 
> 
> Thanks Nitish. In hibernate calling a getter doesn't force initialization,
> at least on many-one fields, but I'll try it out. Since openjpa doesn't
> use cglib that makes sense.
> 
> --James
> 


-----
Pinaki Poddar                      http://ppoddar.blogspot.com/
                                      
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
-- 
View this message in context: http://n2.nabble.com/hibernate-conversion-items-tp2511309p2521187.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


RE: hibernate conversion items

Posted by jlawmi <Ja...@gmail.com>.

Thanks Nitish. In hibernate calling a getter doesn't force initialization,
at least on many-one fields, but I'll try it out. Since openjpa doesn't use
cglib that makes sense.

--James
-- 
View this message in context: http://n2.nabble.com/hibernate-conversion-items-tp2511309p2517170.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


RE: hibernate conversion items

Posted by Nitish Kumar <Ni...@proteans.com>.
Hi James,
        I don't remember reading about any such API in OpenJpa (some body please correct me if there is one), but getting that information should not be that difficult. I just wrote a little API which would get you the information, if the property has been loaded or not. Based on my really simple tests, the API works fine. If you want you can use it for your tests.


public static boolean isPropertyLoaded(Object entity, String property) {
		PersistenceCapable pc = (PersistenceCapable) entity;
		StateManagerImpl sm = (StateManagerImpl) pc.pcGetStateManager();
		ClassMetaData metaData = sm.getMetaData();
		FieldMetaData[] fields = metaData.getFields();
		int dx = 0;
		for (int j = 0; j < fields.length; j++) {
			if (fields[j].getName().equals(property)) {
				dx = j;
				break;
			}
		}
		return sm.getLoaded().get(dx);
	}

For forcing initialization you can just call the getter method. I am sure there are better ways of getting the same information, but this is based on my very limited knowledge of OpenJPA.

Thanks and Regards,
Nitish Kumar

-----Original Message-----
From: jlawmi [mailto:JamesJLaw@gmail.com]
Sent: Sat 3/21/2009 3:12 AM
To: users@openjpa.apache.org
Subject: hibernate conversion items
 

In unit tests (technically integration tests) where I test my mappings,  I
used to call (in hibernate)
Hibernate.isInitialized(someentity.property) to validate things were lazy or
to generally check their load status. Also, Hibernate had
Hibernate.initialize(sometntity.property) to programmatically force
initialization. Is there an easy way to do this in openjpa.? I see the
fetchgroups (which are nice), but configuring a fetchgroup seems overkill in
some cases...

Can someone point me to equivalent items in openjpa?

Thanks!
James
-- 
View this message in context: http://n2.nabble.com/hibernate-conversion-items-tp2511309p2511309.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.



-------------------------------------------------------------------------------------------------------------------------
"The information contained in this e-mail transmission is confidential and may be privileged. It is intended only for the 
addressee(s) stated above. If you are not an addressee, any use, dissemination, distribution, publication, or copying of 
the information contained in this e-mail is strictly prohibited. If you have received this e-mail in error, please 
immediately notify us by telephone (+91 80 6618 6555), or e-mail the sender and delete the e-mail from your system. 
If you do not want to receive our emails please let us know so that we may delete you from our email list. Proteans 
Software Solutions and its parent group ("CAMO Group") do not accept liability for damage caused by this email, and may 
monitor email traffic." 
-------------------------------------------------------------------------------------------------------------------------