openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J Grassel <>
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
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

-- 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);	            
> 	        }
>                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:
Sent from the OpenJPA Users mailing list archive at

View raw message