You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by killbulle <ma...@gmail.com> on 2010/07/21 17:30:23 UTC

Fetch Plan ands collections mapped

Hi,
we have three little questions about fetchplans(we love it)

How can we configure our fetching to get element of a Map ?

addField(CustomerImpl.class, <???>);
Map belong to Customer with this mapping

	@OneToMany(targetEntity = CustomerProfileValueImpl.class,
			   cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@MapKey(name = "localizedAttributeKey")
	@ElementDependent
	@ElementJoinColumn(name = CUSTOMER_UID)
	@ElementForeignKey(name = "tcustomerprofilevalue_ibfk_2")
	public Map<String, AttributeValue> getProfileValueMap() {
		return profileValueMap;
	}

2)Is it possible that fetchplan leave field in lazy mode and not return null
value to the fields unselected ?

3)
In the api there is OPenJPAQuery.getFetchplan there is a getFetplan method
does this fetplan is scoped to the request only ? and more is it possible to
scope fetcplan to only one query

Regards 
Marc

-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5321552.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Fetch Plan ands collections mapped

Posted by killbulle <ma...@gmail.com>.
first thanks for your response 
2. No idea, I simply forbid lazy, why would you leave a lazy attribute ? It
will cause an extra request that has a cost ...
 
it mean that you are  always fetchplan and never graph navigation.
i want leave lazy beahvior because i'am not the owner off the mapping code,
i use a product in openJPA,
my plan is to optimize certain request without breaking the product
behavior.

1) sorry i was a stupid question
 
set mode french on
Si par hasard a novolog vous pouvez proposer une petite formation openJPA
tunning 
pas besoin que ce soit très formalisé nous serions preneurs  
set mon french off
Regards,Cordialement
Marc
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5322952.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Fetch Plan ands collections mapped

Posted by Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com>.
3. Yes, it is possible to activate fetchplan dynamically, for only one request if you want.
2. No idea, I simply forbid lazy, why would you leave a lazy attribute ? It will cause an extra request that has a cost ...
1. I'm not sure in your exact context because we annotate at attribute level rather than accessor level but in our case, you would put the corresponding attribute name in the fetch plan.


On 21 juil. 2010, at 17:30, killbulle wrote:

> 
> Hi,
> we have three little questions about fetchplans(we love it)
> 
> How can we configure our fetching to get element of a Map ?
> 
> addField(CustomerImpl.class, <???>);
> Map belong to Customer with this mapping
> 
> 	@OneToMany(targetEntity = CustomerProfileValueImpl.class,
> 			   cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
> 	@MapKey(name = "localizedAttributeKey")
> 	@ElementDependent
> 	@ElementJoinColumn(name = CUSTOMER_UID)
> 	@ElementForeignKey(name = "tcustomerprofilevalue_ibfk_2")
> 	public Map<String, AttributeValue> getProfileValueMap() {
> 		return profileValueMap;
> 	}
> 
> 2)Is it possible that fetchplan leave field in lazy mode and not return null
> value to the fields unselected ?
> 
> 3)
> In the api there is OPenJPAQuery.getFetchplan there is a getFetplan method
> does this fetplan is scoped to the request only ? and more is it possible to
> scope fetcplan to only one query
> 
> Regards 
> Marc
> 


Re: Fetch Plan ands collections mapped

Posted by Pinaki Poddar <pp...@apache.org>.
>   8: fetchPlan.addField(AbstractCustomer.class, "name"); 
> 14: System.out.println(customer1.getName());
>                
> And if I comment the line 8 : fetchPlan.addField(AbstractCustomer.class,
> "name"); 
> The value of the line 14 is null
> Can you help me ? 

I am not sure whether I understood the problem correctly to be of any help. 

If you comment out Line 8, then "name" is not in the fetch plan. Because few
lines before you cleared the fetch plan even from those fields that are
included by default (such as "name" because it is of type String).
So when Customer is fetched, its "name" is not fetched. You print the name
and see a null. 

Looks normal behavior to me -- but I may be missing something.

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5342383.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Fetch Plan ands collections mapped

Posted by killbulle <ma...@gmail.com>.
I still got a problem

It work with a query but not with a find.

Code:

		1: EntityTransaction transaction = em.getTransaction();
		2: transaction.begin();

		3: OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
		
		4: FetchPlan fetchPlan = kem.getFetchPlan();
		5: fetchPlan.removeFetchGroup(FetchGroup.NAME_DEFAULT);
		6: fetchPlan.clearFetchGroups();
		7: fetchPlan.clearFields();
		
		8: fetchPlan.addField(AbstractCustomer.class, "name");
		
		9: fetchPlan.addField(Order.class, "status");
		
		10: OpenJPAQuery q = OpenJPAPersistence
				.cast(kem
						.createQuery("SELECT c FROM AbstractCustomer c WHERE
c.customerId=:a"));

		11: q.setParameter(1, 2);
		
		12: AbstractCustomer customer1 = (AbstractCustomer) q.getSingleResult();
		
		
		13: AbstractCustomer customer2 = kem.find(AbstractCustomer.class, 2);
		
		14: System.out.println(customer1.getName());
		15: System.out.println(customer2.getName());
		
		16: transaction.rollback();
		17: em.close();

Output:

32  pie-db-JAVA-SE  INFO   [main] openjpa.Runtime - Starting OpenJPA 1.2.2
829  pie-db-JAVA-SE  TRACE  [main] openjpa.jdbc.SQL - <t 21565531, conn
30463067> executing prepstmnt 25709120 SELECT t0.CUSTOMER_ID, t0.type,
t0.name FROM APP.CUSTOMER t0 WHERE (t0.CUSTOMER_ID = ?) [params=(int) 2]
844  pie-db-JAVA-SE  TRACE  [main] openjpa.jdbc.SQL - <t 21565531, conn
30463067> [15 ms] spent
891  pie-db-JAVA-SE  TRACE  [main] openjpa.jdbc.SQL - <t 21565531, conn
10520143> executing prepstmnt 30620267 SELECT t1.ORDER_ID, t1.status,
t1.total, t0.RESIDENTIAL_FREQUENT_CUSTOMER, t0.RESIDENTIAL_HOUSEHOLD_SIZE
FROM APP.CUSTOMER t0 LEFT OUTER JOIN APP.ORDERS t1 ON t0.OPEN_ORDER =
t1.ORDER_ID WHERE t0.CUSTOMER_ID = ? [params=(int) 2]
Roland Barcia
Roland Barcia
891  pie-db-JAVA-SE  TRACE  [main] openjpa.jdbc.SQL - <t 21565531, conn
10520143> [0 ms] spent



And if I comment the line 8 : fetchPlan.addField(AbstractCustomer.class,
"name");
The value of the line 14 is null



Can you help me ?
Thanks

-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5324711.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Fetch Plan ands collections mapped

Posted by Pinaki Poddar <pp...@apache.org>.
As long as you are inside a transaction, the fields are fetched as you access
them -- irrespective of whether they are in the fetch plan or not. Let us
take a concrete example.
Consider an entity X with three fields id and a,b.c (whether they are
single- or multi-valued is not relevant in this context). 

  1: em.getTransaction().begin();
  2: FetchPlan plan = em.getFetchPlan();
  3: plan.addField("a");
  4: plan.addField("b");
  5: X x = em.find(X.class, 1234);
     // The line above will cause OpenJPA to issue a SQL and fetch field id,
a and b.
     // So at this point X will have field id=1234, and field a and b
populated while c == null. 
  6: C c = x.getC();
     // This will cause OpenJPA to issue a separate SQL query to fetch the
value of C.
     // Because the call is made inside an active transaction.
     // even if c was a basic field of say type int or String
  7: em.getTransaction().commit();
  8: em.close()
  
   Had the the transaction been  committed after line 5, the instance x will
become "unmanaged".
The state of x after the transaction has ended will only contain value for
id,a,b but not c. If x.getC() is called, it will return a null -- because at
that point there is no connection to database anymore to fetch the data for
C.

   

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5323453.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Fetch Plan ands collections mapped

Posted by killbulle <ma...@gmail.com>.
first thank for your response,
not only openJPA is fun me, but the forum is friendly.

>>Donot understand the question properly. 
me too, and my english is poor ;)
i will try a simple one.
say that inside a transaction, i select a Customer with a fetchplan which
only return the Customer name.
if a try to get the firstname(or an adress List) il will get null value.
my question: is there a magic to have a  lazyloading behavior for this
field(firstname or adressList)
( so in case off a  object traversal navigation null don't explode)
Regards marc
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5323124.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Fetch Plan ands collections mapped

Posted by Pinaki Poddar <pp...@apache.org>.
> 1)Is it possible that fetchplan leave field in lazy mode and not return
null value to the fields unselected ? 

Donot understand the question properly. But if the question is how the
serialized state of an entity will be (when, say, the entity is accesses and
then detached and serialized to web tier)  w.r.t. its multi-valued
associations, then the answer is the serialized state and behavior is
controlled by the fact whether the remote client has access to OpenJPA
libraries. If yes, then OpenJPA will use a serialization technique that will
allow the user to control the behavior when a multi-valued association is
accessed in remote tier. It can be controlled to disallow access to any
fields that has not been a part of the fetch plan or return a null value.

> In the api there is OPenJPAQuery.getFetchplan there is a getFetplan method
does this fetplan is scoped to the request only ? and more is it possible to
scope fetcplan to only one query

An EntityManager E statrts with a FetchPlan say F0. When E creates a query Q
, Q initially has the the copy of the plan F0. But the plan on Q can be
changed to say F1. It does not affect E's plan. Now E's plan F0 can be
modified to say F0'. The another query Q2 is created. Now Q2 will have a
copy of plan F0'. 

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Fetch-Plan-ands-collections-mapped-tp5321552p5321795.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.