openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J Grassel <fyrew...@gmail.com>
Subject Re: @OrderBy not work?
Date Mon, 21 Sep 2009 15:16:57 GMT

Hmm.  Can you print some pseudocode demonstrating how you are originally
populating the relationship field?  It looks like B is the owner of the
relationship here, so what is set in B's relationship field is what gets
persisted to the database.  This means that in order to establish a
relationship that will persist to the database between A and B, you have to
use B.setF4() method -- calling a.getP3().add(bEntity) will only create an
in-memory relationship which doesn't get saved to the database because A is
on the inverse, not owning, side of the relationship.

2.1.7 Entity Relationships
<snip>
Relationships may be bidirectional or unidirectional. A bidirectional
relationship has both an owning
side and an inverse side. A unidirectional relationship has only an owning
side. The owning side of a
relationship determines the updates to the relationship in the database, as
described in section 3.2.3.

That said, there is an option you can use: OpenJPA's inverse manager.  It is
documented in the OpenJPA manual at
http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/ref_guide_inverses.html

-- be aware that it is expensive in terms of computational resources, and in
most situations it's probably more efficient for the application to manage
the in-memory relationship modeling then to burden OpenJPA with the task.



Forrest Xia wrote:
> 
> 
> However, I got another problem, I cannot get A's B list through A's
> getCollectionRelationship() method, it always return an empty list. Seems
> the OpenJPA does not automatically populate A's B list when I retrieve A
> from database. Can you suggest how to make that? thanks a lot!
> 
> Here is the more complete sample code of how I am using it:
> @Entity
> public class B {
>   @Id
>   private f1;
>   @Column
>   private f2;
>   @Column
>   private f3;
>   @JoinColumn(name = "f4_p1")
>   @ManyToOne(fetch=FetchType.LAZY, optional=false)
>   private A f4;
> }
> 
> @Entity
> public class A {
>   @Id
>   private p1;
>   @Column
>   private p2;
>   @OneToMany(mappedBy= "f4", 
> 			fetch=FetchType.LAZY)
>   @OrderBy("f3 DESC")
>   private Collection&lt;B&gt; p3;
> }
> 
> public class C {
>   private EntityManager em;
>   public Collection&lt;B&gt; listB(String aKey) {
> 		
> 		A a = em.find(A.class, akey);
>                // a.getP3() always return an empty list here, why?
>                Collection&lt;B&gt; bs = a.getP3();
>                Iterator itr = bs.iterator();
> 		while (itr.hasNext()) {
> 	            ((B) itr.next()).getF1();	            
> 	        }
>                return bs;		
> 	}
> }
> 
> 
> 
> J Grassel wrote:
>> 
>> Whoops, commented out too many lines in the second example.  Hope it is
>> still clear on what I was trying to communicate.
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/OrderBy-not-work-tp3663648p3686022.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message