-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Query.list() return proxy
PostPosted: Tue Feb 15, 2011 9:05 pm 
Newbie

Joined: Tue Feb 15, 2011 8:53 pm
Posts: 2
hi,is there someone tell me when Query.list() return proxy??
I had a problem,my code is below
Code:
@SuppressWarnings("unchecked")
   public List<Template> find(String hql, Object... values) {
      Query q = createQuery(hql, values);
      return q.list();
   }

the createQuery method is:
Code:
/**
    *
    * @param hql
    * @param values
    * @return
    */
   protected final Query createQuery(String hql, Object... values) {
      Query query = getSession().createQuery(hql);
      if (null != values) {
         for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
         }
      }
      return query;
   }

after call find() method,will auto commit the session.
i use the find() in struts action,but it return a list of proxy,and the proxy's initialized is true.
the template object has id,name,content property,id is the primary key,in web page,it can get the name,but cann't get the id...
i am very very puzzle.thanks you reply


Top
 Profile  
 
 Post subject: Re: Query.list() return proxy
PostPosted: Wed Feb 16, 2011 5:00 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
>>when Query.list() return proxy??

If Query.list() returns hibernate proxies or not, depends exclusively on how the first access on each single entity in your persistent context happens or happened. Once a entity is first accessed as hibernate-proxy, on further accesses within your persistent context it always will continue to be returned as hibernate-proxy.
So if the result-set of your query contains an entity which is already have been loaded previously in your persistent context as hibernate-proxy, then also in your result-set this entity will appear as hibernate-proxy.

The reason for this behavior is to guarantee the object-identity within the persistent context.
To maintain the object identity contract within a transaction, Hibernate tries to expose to the
programmer a determinate entity either always as proxy or always as real object,
it depends only from the way how it is accessed for the first time in your persistent context.
seehttp://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/transactions.html#transactions-basics-identity


Last edited by pb00067 on Tue Feb 21, 2012 6:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Query.list() return proxy
PostPosted: Wed Feb 16, 2011 9:16 pm 
Newbie

Joined: Tue Feb 15, 2011 8:53 pm
Posts: 2
pb00067 wrote:
>>when Query.list() return proxy??

If Query.list() returns hibernate proxies or not, depends exclusively on how the first access on each single entity in your persistent context happens or happened. Once a entity is first accessed as hibernate-proxy, on further accesses within your persistent context it always will continue to be returned as hibernate-proxy.
So if the result-set of your query contains an entity which is already have been loaded previously in your persistent context as hibernate-proxy, then also in your result-set this entity will appear as hibernate-proxy.

The reason for this behavior is to guarantee the object-identity within the persistent context.
To maintain the object identity contract within a transaction, Hibernate tries to expose to the
programmer a determinate entity either always as proxy or always as real object,
it depends only from the way how it is accessed for the first time in your persistent context.
seehttp://docs.jboss.org/hibernate/stable/core/reference/en/html/transactions.html#transactions-basics-identity


thanks for you reply.
with your help,i sovled the problems.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.