google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

how to retreive @Reference fields / which is best @Reference or @Embedded and SubQuery in MorphiaQuery #463

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Version: (Morphia Query)

I have a mongodb with two model classes say **User** and **UserInfo**.... The 
criteria is in **User** class  I have to retrieve a multiple fields around 10 
fields like **"firstName","lastName",etc** and in **UserInfo** Model class I 
like to retrieve only one field say "**age**", At this moment I referenced the 
UserInfo class's object to the User class like stated below in the 
**Structure** and its stores in the DB as  
**{"firstName","John"},{"lastName","Nash"},{userInfo: userInfoID}** but if I 
make an **Embedded** Relation then it would store all the userInfo's fields and 
I think to retrieve one ("age") field it is Unwanted to Embed all the 
userInfo's fields which inturn will make the application slow I think, Can you 
please Guide me which scenario to use whether @Reference or @Embedded , I think 
Embedded will slow down my response to DB but in the websites its given as 
reference annotation only slows down querying time and needs some sort of 
**Lazy Loading** an all, my structure is like below:    

    class User extends Model{
     public String firstName;
     public String lastName;
     public String loginTime;
     public String logoutTime;
     public String emailId; etc,etc......
      Some more 10 fields like this+userInfo reference object 
      @Reference
      public UserInfo userInfo;
      }   
      class UserInfo extends Model{
     public String emailId;
     public String age;
     public String sex;
     public String address;
     public String bank; etc,etc......
     Some more 10 fields like this
      }   

As I stated above I want only **age** field from **UserInfo** and **all** 
fields of **User**, so which Annotation is best and @Reference or @Embedded. It 
will be  more helpful if **I get a single query for User class in which I can 
retrieve all fields of User and only "age" field of UserInfo** , To tell in 
short I need a **query like this** when I go for @Reference relationship 

    field("userInfo.age") for userInfo.emailId = (MorphiaQuery q =        User.createMorphiaQuery;           
     q.field("firstName").equal("John");     q.field("lastName").equal("Nash");                                             q.field("loginTime").greaterthan("sometime"))//the complex part where I need age of particular userInfo but I have only the ID of the userInfo since Iam using Reference and that Id too got from a **subQuery**.... 

**Please dont write two queries I need a single query or maybe a query with 
subquery:
       To be more clear I can tell in SQL language :** 

       SELECT age FROM UserInfo where emailId = u.emailId
       (SELECT * FROM User WHERE firstName='John' AND lastName='Nash' AND
           logintime='someTime') AS u;    

I  need this exact same query without writing two morphia quries which consumes 
more time by referring two tables so please guide me pls reply **ASAP** ... 
Thanks in Advance  :There is nowhere in the Internet I could find the Answer, 
So please some **genius** guide me or give me a **proper Answer**:)    little 
**URGENT** please !!!!

Original issue reported on code.google.com by vignesh....@gmail.com on 31 Jul 2013 at 8:44