Which is better of MANY_TO_ONE and ONE_TO_MANY

I am trying je.
A little confuse is , that suppose A has one_to_many relation to B, then B can have many_to_one relation to A. And both solution is acceptable to my application. Which should I choose?

Hello Jie,
This is a very good question, since we do not describe in the documentation how to choose between MANY_TO_ONE and ONE_TO_MANY. As you point out, it can be done either way when a related entity is used.
For example, here is how MANY_TO_ONE can be used:
@Entity
class Person {
  @PrimaryKey(sequence="ID")
  long id;
  @SecondaryKey(relate=MANY_TO_ONE, relatedEntity=Employer.class)
  long employerId;
  String name;
@Entity
class Employer {
  @PrimaryKey(sequence="ID")
  long id;
  String description;
}Of course we will need primary indexes for both entities:
PrimaryIndex<Long,Person> personById = ...;
PrimaryIndex<Long,Employer> employerById = ...;To get the employer of a person, we simply pass the employerId to the primary index:
Person person = ...;
Employer employer = employerById.get(person.employerId);To get the employees for a given employer, we get a sub-index for the employerId secondary key:
long anEmployerId = ...;
SecondaryIndex<Long,Long,Person> personByEmployerId = ...;
EntityIndex<Long,Person> employees = personByEmployerId.subIndex(anEmployerId);
EntityCursor<Person> cursor = employees.entities();
try {
  for (Person employee : cursor) {
    // do something with employee
} finally {
  cursor.close();
}And here is how ONE_TO_MANY can be used to provide the same functionality:
@Entity
class Person {
  @PrimaryKey(sequence="ID")
  long id;
  String name;
@Entity
class Employer {
  @PrimaryKey(sequence="ID")
  long id;
  @SecondaryKey(relate=ONE_TO_MANY, relatedEntity=Person.class)
  Set<Long> employeeIds;
  String description;
}To get the employer of a person, we simply pass the person id to the employer's secondary index:
Person person = ...;
SecondaryIndex<Long,Long,Employer> employerByEmployeeIds = ...;
Employer employer = employerByEmployeeIds.get(person.id);The primary difference is that with MANY_TO_ONE the collection of employee IDs is maintained by the secondary index, while with ONE_TO_MANY the collection of employee IDs is embedded in the Employer entity.
Therefore to get the employees for a given employer with ONE_TO_MANY, we get the Employer object from the employerById primary index and then get each person in the employeeIds set:
long anEmployerId = ...;
SecondaryIndex<Long,Long,Person> personByEmployerId = ...;
Employer employer = employerById.get(anEmployerId);
for (long employeeId : employer.employeeIds) {
  Person employee = personById.get(employeeId);
  // do something with employee
}There is a major difference in terms of performance and scalability. With ONE_TO_MANY, if the number of employees for a single employer is very large, then the Employer record will be large because the list of employee IDs is embedded in the Employer object. This means that it will be more expensive to load the employer, for example, if all you want is the employer description field. The list of employee IDs will always be loaded whenever you access an employer.
On the other hand, with MANY_TO_ONE, the set of employers is stored in the sub-index. The sub-index is a Btree. It is very scalable and can easily support large numbers of employees per employer, even millions. And when simply loading the Employer record, the set of employeeIds for that employer will not be loaded. The Employer record will be very small.
There are four factors in deciding whether to use MANY_TO_ONE or ONE_TO_MANY.
(1) As shown above, it is slightly easier to use ONE_TO_MANY, because you don't need to use a sub-index as you must do with MANY_TO_ONE.
(2) MANY_TO_ONE will scale to larger number of entities per relationship, since a sub-index Btree is used to store the related entities.
(3) With MANY_TO_ONE, the entity object will not grow to a large size as many related entities are added. Smaller records are always better for performance and resource consumption.
In general, I advise using MANY_TO_ONE because it does not have the potential performance problems of ONE_TO_MANY (items 2 and 3 above).
But if you know that the number of related entities per relationship will be small, then either technique works well, and you should choose whichever you prefer. You may find that ONE_TO_MANY is slightly easier to use (item 1 above). Or you may choose one technique over the other because you prefer to maintain the relationship via the Person.employerId field or the Employer.employeeIds set.
There is one additional factor. If you have a secondary key where there is no related entity, just related keys, then you have no choice. There is only one entity, and you choose the relationship type accordingly.
For example, a Person may have many email addresses, and you may choose not to have an EmailAddress entity. The email addresses are simply secondary keys, not the key of another related entity. If a user can only have one email address, you would use ONE_TO_ONE. If the user can have multiple email addresses, you would use ONE_TO_MANY as shown below.
@Entity
class Person {
  @PrimaryKey(sequence="ID")
  long id;
  @SecondaryKey(relate=ONE_TO_MANY)
  Set<String> emailAddresses;
  String name;
}A gender (male or female) secondary key is an example of a MANY_TO_ONE relationship where no related entity is involved:
@Entity
class Person {
  @PrimaryKey(sequence="ID")
  long id;
  @SecondaryKey(relate=MANY_TO_ONE)
  char gender; // M or F
  String name;
}In this last example, there is no choice about whether to use ONE_TO_MANY or MANY_TO_ONE because there is only one entity involved. So this last rule is:
(4) If there is no related entity, then because there is only one entity involved the relationship type is determined by the cardinality (number) of entities and keys in each relationship.
Mark

Similar Messages

  • Which is better for gaming and why?

    Hi,
    I was researching budget gaming laptops(around $1000, INR 55000) and came up with two choices.....
    1)HP Pavilion g6z-1d00 Notebook PC
    http://shopping2.hp.com/en_US/home-office/-/products/Laptops/HP-Pavilion/A2B39AV?HP-Pavilion-g6z-1d0...
    2)HP Envy 14-2130nr Notebook PC
    http://shopping2.hp.com/en_US/home-office/-/products/Laptops/HP-ENVY/QE295UA?HP-ENVY-14-2130nr-Noteb...
    Could anybody please tell me which is better for gaming (main purpose and best performance)?
    The HP Envy looks great but I don't know much about AMD Quad Core.....
    Also, is it possible to upgrade the graphics card on the HP Envy ( AMD Radeon 6630 to a better one) via a HP centre?
    It seems to run most games quite well, but I need it to be able to play new games for at least 4 years more (college), while not factoring in the costs for any possible graphics card upgrade....
    Please help!!

    Thanks Huffer _ very useful.
     I wasn't aware that there was a heat problem with the i7 processors, my dv8 runs at around 42 to 52 degrees, mind you I don't really put it under much load. I use it to play Blu-ray ISO files on my projector and for photo editing (Adobe CS4), it's an 820QM.
     I have also changed the thermal compound to ICDiamond and drilled holes in the speaker cover to improve air flow, that made a difference too.
     @ abhishekmaha
     Don't take my comment to seriously, although I think it has some truth to it, it was meant to poke a bit of fun at you. 
     Heat has always been one of the biggest factors when it comes to any computer, designers try to make them thinner, lighter and cheaper and those types don't work for gaming very well. They're OK for the occasional one, but not on a regular basis. I believe that if you game on any machine that was not meant for it, you will not get 4 years out of it _ 2 years.
     I wish I could tell you different, but I am not going to lie to you.
     Heat basically affects 3 things on a laptop, the solder that holds the components on the motherboard, any mechanical moving parts, eg the hard drive, fan _ and the battery. You can remove the battery to prolong its life when gaming.
     Heat breaks down the solder over time and the mother board simply stops working as far as the hard drive, it stops spinning because the tiny bearing(s) in there dry out and seize up.
     If you don't want to use a cooling mat then prop up the back of the laptop, hanging it over the edge of the table will expose the fan intake, but will block off other vents.
     60 degrees is a bit warm too, 50 is better or rather, the cooler the better. What ever machine you get, make a note of what the temps are when you first boot up and when it's hot. The idea is to get an average so you can judge when it is dangerously getting hot.
     P.S. Be sure and give Huffer a kudos too.

  • Which is better for school and notes pages or ms word

    Hi have a macbook white and i bring this to school and i want to know which is better to use ms word or pages for notes and school work

    If you have used Windows for years and Microsoft Office, then Word will work much better because you will learn to use it quicker than Pages. However, Pages is much cheaper than Microsoft Word, and if you don't mind learning a new interface, then Pages should be enough.
    I use Pages for school projects, and there's one thing that makes a difference: create equations. Pages requires a third-party application like MathType and Word has got a built-in editor, so you should consider this

  • Which is better for computer and program... sleep or turn off?

    When not using, which is better for the program, sleep or turn off the computer and any explanations are welcome...

    I advise shutting down rather than sleep. A good shut down often clears up nascent problems, and of course you're saving electricity by not running your machine and also saving those precious MTBF hours on all the mechanical components. Think of the hours it would sleep vs. the number of hours you actually use the machine. Even though sleep consumes less electricity, it still consumes.

  • Nvidia Quadro K4000 vs Asus GTX 760. Which one better for AE and Pr ?

    I have 2 Computers and planning to mix them up together as possible. I built a PC with quadro k4000 last year because I was graduated as 3D animator and i plan to work as Animator, plus I also have another PC with GTX 760. and now I'm currently working as Video Editor. so here are my questions:
    - k4000 and gtx 760 which one would do better in AE and Pr ?
    - I read some articles said: "quadro and gtx can group up together in one PC". is it really possible ? because some people said yes and some people said no and some people said yes but wont work together blah blah. I really want to know about it, because right now I already have these 2 GPU. and if YES. can you show me how to do it?
    there are my both PC specs:
    CPU: core i7 3820 3.66hz
    RAM: 16GB
    SSD 128gb
    HDD 3TB
    MotherBoard: Asus P9X79 PRO
    VGA: asus GTX 760
    Another one is:
    CPU core i7-3930K 3.20GHz
    RAM: 32GB
    SSD: 256gb
    HDD: 3TB x 2
    MotherBoard:  ASRock X79 Extreme6
    VGA: quadro k4000
    Can somebody help me to build these 2 PC in just 1 PC as possible ?

    I have something similar that I just posted about - my system has both a GTX780 and a Quadro K4200 installed. Eric is right. They work, but the driver installation can be tricky.
    Mine is a new build and I am trying to learn how the two cards work together. I expected better performance. I don't understand why setting CUDA acceleration to only the GeForce, when NVidia allows for both CUDA GPUs. I tried every combination with no difference in results. So, I will be following your thread to see what happens.

  • Which is better for range and speed of wifi, time capsule A1254 or the new airport express? I don't need the hard drive aspect of the time capsule. Thanks!

    Thanks for any help with this.

    You are asking several different questions. If you need to store your photos, music, and movies on an external volume, you certainly can. Any externally connected hard disk drive will work, connected either directly to your Mac or to your Time Capsule as a shared volume.
    You should not rely upon using that as a backup device though. Although you certainly may use it for both purposes, it is a better idea to have dedicated backup devices for a variety of reasons not limited to redundancy. You would not want to simultaneously lose all your pictures as well as your backup. If they are all on the same device, that could happen. Furthermore, a backup cannot back up the volume on which it is running.
    As for adding an Extreme or Express, using its LAN port for your iMac, and then enable Internet sharing so you can effectively use the iMac as a "hotspot", you can do that too, but I am unclear on what benefit you believe this arrangement would convey for you.
    An Extreme's Guest network is separate from its Main network; that is the reason for having it.

  • Display 128MB v. 256 memory - which is better for CS2 and FCP

    Hi, there was a similar post to this a while back that did not adequately answer the question that I now have. I am deciding between the base model and mid-level 15 inch MacBook Pro:
    1) What does the 128MB v. 256 MB video memory directly affect?
    Is this important mostly for gamers? Video editors? Downloading graphics intense web sites?
    I work on Photoshop, Capture One (for on-set digital photo catpure), Flash, and Final Cut Pro. How much will I notice the difference between the 128MB and 256MB memory in these applications?
    Is it worth the extra money between the 2GHz and the 2.16 GHz MacBook Pro?
    Thanks.

    Currently, 128MB is more than enough for Photoshop and/or FCP. For each of these apps, the video card merely draws the image on the screen. Aperture and Motion on the other hand, make heavy use of the graphics card and the more MB the better. While you may not use Aperture or Motion, keep in mind that future releases of different software may make more use of the graphics card. As far as 2.0 vs 2.16, I can't possible imagine how anyone would ever be able to tell a difference in day-to-day productivity. I have both a D1.8 and a D2.7 and the D2.7 is quite a bit faster at rendering, compressing, batch processing and so on; but editing a project or working on images within Photoshop, I couldn't tell you which is faster.
    Mike

  • Linux on Primary Or Logical? which is better?? and why?

    hello,
    what is the best way to install linux ( redhat,oracle enterprise ,etc)
    on primary partition or logical partition ? and why?
    i have 3 hd's ( hda,hdb,hdc) >> ( 40 GB,80GB,320GB) IDE
    why im asking this becoz i installed linux more than 3 times lately and i always ended up with GRUB errors and problems
    any idea plz shed some light on this
    need your experience regarding the optimal solution to install linux along with Windows OS
    thanks and regards,

    Hi,
    Q: what is the best way to install Linux ( redhat,oracle enterprise ,etc)
    A: Almost no difference between Redhat and Oracle Enterprise Linux. But Oracle has patched some additional patches for Oracle database based on Redhat released Enterprise Linux. If you would have subscribed Oracle Unbreakable Linux Network(ULN), which make it easier to update the system and deploy Oracle database. For more details refer to http://linux.oracle.com
    Q: On primary partition or logical partition ? and why?
    A: Your boot partition ought to be a primary partition, not a logical partition. So if you want to boot your operating system from the drive you are about to partition, it should be installed on primary partition. Otherwise, you can install it on logical partition.
    Q: why im asking this becoz i installed linux more than 3 times lately and i always ended up with GRUB errors and problems
    A: For this issue, what kind of GRUB error had you experienced? What the result after re-installing the grub?
    Happy new year!
    Terry

  • Which is better for usb camera between VISA and imaqdx in labview 2010?

    i have done a program for usb camera to acquire images with imaqdx.
    now i want to know which is better between VISA and  imaqdx in labview 2010?
    i didn't study visa before
    thans for help.

    VISA is primarily used to communicate with instruments via GPIB/TCP-IP/Serial/USB. In order to use easily VISA with an instrument over USB the instrument must adhere to the USBTMC spec. A camera is not likely to have this. Otherwise you have to use USB RAW, and that requires detailed knowledge of the data that is transmitted by the instrument. Manufacturers rarely give this information out. Normally they will provide a DLL to communicate with the instrument.
    Thus, in your case, VISA is not going to be an appropriate API to use for a USB camera.
    Out of curiosity, what prompted this question?

  • Which is better? Photoshop CS6 or Elements? And why?

    Wife is an avid photographer and likes to be able to fix photos, make collages.  Confused on which is better, Photoshop CS6 (and what is Extended?) or Elements? What are the pros and cons of each? Thank you for your time.

    The Photoshop & Elements users hang out in those software forums.  You should post there.  This area is for MBP technical problems. 

  • Which is better for education iPad or Macbook air?

    I am a grade 8 student and I walk around quite a bit in school and I would like to know which one is better for me?
    Which is better for school, and no I can not get the discount.
    Macbook Air or
    iPad
    If macbook Air which size and specs would you reccomend?
    And if iPad which storage option, white or black do you reccomend?
    And should I buy the iPad 2 or the new one.

    Great question.
    Word processing for iOS is fantastic, but not as good for citations and things as on the macbook air, though footnotes will be preserved.
    The new textbook options on ipad are compelling. You should find out if any of your school textbooks are on there by searching the itunes store. If so, ipad is your choice.
    They are different categories of products. the macbook air is an excellent computer.
    But I guess I feel this way -- why don't you buy a desktop for the home. and an ipad for school? If you feel you can get all of your notetaking, paper drafts etc. done on ipad, then you can use mountain lion (coming out this summer) to keep it all in sync when you go home.
    I do have to warn you that I tried taking my ipad to work. If you use google docs at all, you may find some limitations sometimes. If you stick within the icloud universe, (pages, keynote, and numbers), you may be mostly happy.

  • Songs with the same title, which is better?

    Song titles aren't always unique to one song, there can be many different ones with the same title. This thread is for those kinds of songs. Post them and tell us which is better to you, and why do you think that is.
    For example, I think this song:
    Is better than this one:
    Because I'm a bit more partial to Pulp and Britpop. But I still think Tom Jones' song is great nonetheless.
    Go ahead and give us your opinion!

    For me, I chose two songs called "Living Well; one by American shoegazers Velocity Girl, the other by Texas slowcore group Bedhead. Although I like both, I prefer the Pavement-ish melancholy of the Bedhead song to the sugary rush of the Velocity Girl song. spotify:track:5BrtSevKCfYWuZ6WnBv9Vcspotify:track:1Fu9pnUNBbyTUOsA5ZwsMd

  • I am going to buy a macbook pro for grade 12, and I need to know wheather I should get a macbook pro or a macbook pro retina. If someone could tell me (in a very simple way) which one is,better for me and why, I would be ever so apprreciative.

    I am going to buy a macbook pro for grade 12, and I need to know wheather I should get a macbook pro or a macbook pro retina. If someone could tell me (in a very simple way) which one is,better for me and why, I would be ever so apprreciative.

    Why do you need a expensive MacBook Pro?
    Your attending high school and unless everyone else is rich also your likely going to be a target by the more poorer students for theft or damage to the machine.
    You could keep it home, but if you need it for class then your exposed again.
    Also at that age your not very careful yet, a MacBook Pro is a expensive and easily damaged machine.
    Unless your made of money and so are others at your school, I would recommned a low profile, just does the job cheap Windows PC.
    If it dies, gets lost, stolen or damaged because of your inexperince handling senstivie electronics then it's no big deal.
    You can buy a Mac later on when your sure you have a need for it, currently there isn't much advantage of owning a Mac compared to a PC, they do just about the same things now, one just looks prettier than the other.
    Since 95% of the world uses Windows PC's your going to have to install Windows on the Mac in order to keep your skills up there or be unemployed, so it's a extra headache and expense.
    good luck

  • Which is better software for brochures and PDF forms ? Photoshop or InDesign ?

    Which is better software for brochures and PDF forms ? Photoshop or InDesign ? and why ?

    If you are going to be making a lot of brochures, with photos, you probably want both. Photoshop to edit the photos, and InDesign to assemble and layout images with text.

  • Which is better and why: Safari or Firefox?

    Which is better and why: Safari or Firefox?

    Thx, I've been using FF on my MacBook, as I did with PowerMacs before that. I used to work in the industry and stay up on current trends; but, I'm just a regular user with a new MacAir now and hate to bother my SF/SJ peeps with trivial questions.
    As I start to load up the Air, I'm just a bit overwhelmed with the volume of bookmarks I've collected and organzied in FF on my MB.

Maybe you are looking for